Android實現(xiàn)帶圖標的列表對話框
根據(jù)之前學(xué)的Android對話框技術(shù),來實現(xiàn)下面一個效果:界面有一個"打開設(shè)置對話框"按鈕,將彈出選擇項目的對話框,單擊任意列表項,都將關(guān)閉該對話框,并通過消息提示框顯示選擇的列表內(nèi)容。
效果如圖所示:
實現(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è)置對話框"/> </LinearLayout>
編寫用于布局列表項內(nèi)容的XML布局文件items.xml,在該文件中,采用水平線形布局管理器,并在該布局管理器中添加ImageView組件和一個TextView組件,分別用于顯示列表項中的圖標和文字。
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和列表項文字放到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è)置對話框的圖標 builder.setTitle("設(shè)置:");//設(shè)置對話框的標題 //添加列表項 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)建對話框并顯示 } }); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中自定義對話框(Dialog)的實例代碼
- Android實現(xiàn)點擊AlertDialog上按鈕時不關(guān)閉對話框的方法
- 實例詳解Android自定義ProgressDialog進度條對話框的實現(xiàn)
- android 對話框彈出位置和透明度的設(shè)置具體實現(xiàn)方法
- Android實現(xiàn)底部對話框BottomDialog彈出實例代碼
- Android中AlertDialog各種對話框的用法實例詳解
- Android加載對話框同時異步執(zhí)行實現(xiàn)方法
- Android修改源碼解決Alertdialog觸摸對話框邊緣消失的問題
- Android中 動態(tài)改變對話框值的方法
- Android使用setCustomTitle()方法自定義對話框標題
相關(guān)文章
adnroid已安裝應(yīng)用中檢測某應(yīng)用是否安裝的代碼實例
這篇文章主要介紹了Android怎么檢測一個應(yīng)用是否安裝的方法,大家參考使用吧2013-11-11Android學(xué)習(xí)筆記之應(yīng)用單元測試實例分析
這篇文章主要介紹了Android學(xué)習(xí)筆記之應(yīng)用單元測試,結(jié)合實例形式較為詳細的分析了Android單元測試的實現(xiàn)原理與具體步驟,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11淺析Android手機衛(wèi)士關(guān)閉自動更新
保存數(shù)據(jù)的四種方式,網(wǎng)絡(luò),廣播提供者,SharedPreferences,數(shù)據(jù)庫。接下來通過本文給大家介紹android手機衛(wèi)士關(guān)閉自動更新的相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧2016-04-04FragmentTabHost FrameLayout實現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細介紹了FragmentTabHost和FrameLayout實現(xiàn)底部導(dǎo)航欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03基于linux與windows平臺下 如何下載android sdk源代碼的方法詳解
本文主要是介紹在linux和windows平臺下,如何下載android sdk的源代碼,注意是sdk的源代碼,而不是android的所有源代碼,同時介紹如何把sdk源代碼加入到eclipse里,使android 平臺手機開發(fā)者可以直接查看源代碼,通過閱讀SDK源碼,能更好的理解和運用Android的API2013-05-05Android學(xué)習(xí)項目之簡易版微信為例(二)
這篇文章主要以簡易版微信為例,實現(xiàn)簡易版微信的登陸、注冊界面的編寫與簡單交互,感興趣的小伙伴們可以參考一下2016-06-06Android ListView 滾動條的設(shè)置詳解及實例代碼
這篇文章主要介紹了 ListView等滾動條的設(shè)置詳解詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Android 5.0及以上編程實現(xiàn)屏幕截圖功能的方法
這篇文章主要介紹了Android 5.0及以上編程實現(xiàn)屏幕截圖功能的方法,結(jié)合實例形式分析了Android5.0以上實現(xiàn)截圖功能的相關(guān)類、函數(shù)及權(quán)限控制等操作技巧,需要的朋友可以參考下2018-01-01