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ì)話框并顯示
}
});
}
}
以上就是本文的全部內(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 優(yōu)雅的實(shí)現(xiàn)通用格式化編輯
這篇文章主要介紹了Android 優(yōu)雅的實(shí)現(xiàn)通用格式化編輯,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
探討Android 的屏幕滾動(dòng)操作不如 iPhone 流暢順滑的原因
雖然很多Android手機(jī)的配置都比iPhone要高,比如大多數(shù)Andorid手機(jī)的內(nèi)存都有1GB,而iPhone 4S只有512MB內(nèi)存,但用過iPhone的人都知道Android手機(jī)在使用的時(shí)候總感覺沒有那么順滑,究竟為什么會(huì)出現(xiàn)這種現(xiàn)象呢?2014-07-07
TableLayout(表格布局)基礎(chǔ)知識(shí)點(diǎn)詳解
在本文里我們給大家分享了關(guān)于TableLayout(表格布局)的相關(guān)基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-02-02
Android中系統(tǒng)默認(rèn)輸入法設(shè)置的方法(輸入法的顯示和隱藏)
這篇文章主要介紹了Android中系統(tǒng)默認(rèn)輸入法設(shè)置的方法(輸入法的顯示和隱藏)的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android開發(fā)中TextView文本過長滾動(dòng)顯示實(shí)現(xiàn)方法分析
這篇文章主要介紹了Android開發(fā)中TextView文本過長滾動(dòng)顯示實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android項(xiàng)目開發(fā)中TextView顯示超長文本的具體操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-02-02
Android 百度地圖marker中圖片不顯示的解決方法(推薦)
下面小編就為大家分享一篇Android 百度地圖marker中圖片不顯示的解決方法(推薦),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01

