Android實(shí)現(xiàn)帶圖標(biāo)的列表對(duì)話框
根據(jù)之前學(xué)的Android對(duì)話框技術(shù),來實(shí)現(xiàn)下面一個(gè)效果:界面有一個(gè)"打開設(shè)置對(duì)話框"按鈕,將彈出選擇項(xiàng)目的對(duì)話框,單擊任意列表項(xiàng),都將關(guān)閉該對(duì)話框,并通過消息提示框顯示選擇的列表內(nèi)容。
效果如圖所示:

實(shí)現(xiàn)代碼:
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout1"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打開設(shè)置對(duì)話框"/>
</LinearLayout>
編寫用于布局列表項(xiàng)內(nèi)容的XML布局文件items.xml,在該文件中,采用水平線形布局管理器,并在該布局管理器中添加ImageView組件和一個(gè)TextView組件,分別用于顯示列表項(xiàng)中的圖標(biāo)和文字。
res/layout/items.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:paddingLeft="10px"
android:paddingTop="20px"
android:paddingBottom="20px"
android:adjustViewBounds="true"
android:maxWidth="72px"
android:maxHeight="72px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10px"
android:layout_gravity="center"
android:id="@+id/title"/>
</LinearLayout>
MainActivity:
package com.example.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int[] imageId=new int[]{R.drawable.in,R.drawable.stop,
R.drawable.setting,R.drawable.music,R.drawable.exit};
final String[] title=new String []{"程序管理","保密設(shè)置","安全設(shè)置","郵件設(shè)置","鈴聲設(shè)置"};
List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
//通過for循環(huán)將圖片id和列表項(xiàng)文字放到map中,并添加到List集合中
for (int i = 0; i < imageId.length; i++) {
Map<String,Object> map=new HashMap<String,Object>();
map.put("image", imageId[i]);
map.put("title", title[i]);
listItems.add(map);
}
final SimpleAdapter adapter=new SimpleAdapter(this,listItems,
R.layout.item,new String[]{"title","image"},new int[]{R.id.title,R.id.image});
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.music);//設(shè)置對(duì)話框的圖標(biāo)
builder.setTitle("設(shè)置:");//設(shè)置對(duì)話框的標(biāo)題
//添加列表項(xiàng)
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您選擇了【"+title[which]+"】", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();//創(chuàng)建對(duì)話框并顯示
}
});
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- android 對(duì)話框彈出位置和透明度的設(shè)置具體實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android加載對(duì)話框同時(shí)異步執(zhí)行實(shí)現(xiàn)方法
- Android修改源碼解決Alertdialog觸摸對(duì)話框邊緣消失的問題
- Android中 動(dòng)態(tài)改變對(duì)話框值的方法
- Android使用setCustomTitle()方法自定義對(duì)話框標(biāo)題
相關(guān)文章
Android中進(jìn)程生命周期的優(yōu)先級(jí)
這篇文章主要介紹了Android中進(jìn)程生命周期的優(yōu)先級(jí)的相關(guān)資料,需要的朋友可以參考下2016-01-01
adnroid已安裝應(yīng)用中檢測(cè)某應(yīng)用是否安裝的代碼實(shí)例
這篇文章主要介紹了Android怎么檢測(cè)一個(gè)應(yīng)用是否安裝的方法,大家參考使用吧2013-11-11
Android學(xué)習(xí)筆記之應(yīng)用單元測(cè)試實(shí)例分析
這篇文章主要介紹了Android學(xué)習(xí)筆記之應(yīng)用單元測(cè)試,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android單元測(cè)試的實(shí)現(xiàn)原理與具體步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
淺析Android手機(jī)衛(wèi)士關(guān)閉自動(dòng)更新
保存數(shù)據(jù)的四種方式,網(wǎng)絡(luò),廣播提供者,SharedPreferences,數(shù)據(jù)庫(kù)。接下來通過本文給大家介紹android手機(jī)衛(wèi)士關(guān)閉自動(dòng)更新的相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2016-04-04
FragmentTabHost FrameLayout實(shí)現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了FragmentTabHost和FrameLayout實(shí)現(xiàn)底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android實(shí)現(xiàn)多線程斷點(diǎn)下載
大家好,本篇文章主要講的是Android實(shí)現(xiàn)多線程斷點(diǎn)下載,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
基于linux與windows平臺(tái)下 如何下載android sdk源代碼的方法詳解
本文主要是介紹在linux和windows平臺(tái)下,如何下載android sdk的源代碼,注意是sdk的源代碼,而不是android的所有源代碼,同時(shí)介紹如何把sdk源代碼加入到eclipse里,使android 平臺(tái)手機(jī)開發(fā)者可以直接查看源代碼,通過閱讀SDK源碼,能更好的理解和運(yùn)用Android的API2013-05-05
Android學(xué)習(xí)項(xiàng)目之簡(jiǎn)易版微信為例(二)
這篇文章主要以簡(jiǎn)易版微信為例,實(shí)現(xiàn)簡(jiǎn)易版微信的登陸、注冊(cè)界面的編寫與簡(jiǎn)單交互,感興趣的小伙伴們可以參考一下2016-06-06
Android ListView 滾動(dòng)條的設(shè)置詳解及實(shí)例代碼
這篇文章主要介紹了 ListView等滾動(dòng)條的設(shè)置詳解詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法
這篇文章主要介紹了Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法,結(jié)合實(shí)例形式分析了Android5.0以上實(shí)現(xiàn)截圖功能的相關(guān)類、函數(shù)及權(quán)限控制等操作技巧,需要的朋友可以參考下2018-01-01

