Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對(duì)話框功能
在使用AlertDialog實(shí)現(xiàn)單選和多選對(duì)話框時(shí),分別設(shè)置setSingleChoiceItems()和setMultiChoiceItems()函數(shù)。
下面看主要的代碼:
數(shù)據(jù)源數(shù)組:
<resources> <!--單選--> <string-array name="arr_weather"> <item >晴</item> <item >多云</item> <item >小雨</item> <item >中雨</item> </string-array> <!--多選--> <string-array name="arr_grasslandGreatType"> <item >羊草</item> <item >牛草</item> </string-array> </resources>
Activity中的主要代碼:
點(diǎn)擊事件:
case R.id.edt_sampleWeather:// 天氣選取 String[] arrWeather = getResources().getStringArray(R.array.arr_weather); showAlertDialog(arrWeather, selectWeatherId, 0, tv_sampleWeather); break; case R.id.edt_grasslandGreatType:// 草地優(yōu)勢(shì)種選擇 showMultiDialog(); break;
對(duì)應(yīng)方法:
(1)showAlertDialog()方法,實(shí)現(xiàn)單選效果,selectWeatherId 設(shè)置選定的條目位置
private void showAlertDialog(final String[] items, int selectId, final int type, final TextView tView) { AlertDialog.Builder builder = new AlertDialog.Builder(CreatePointActivity.this); builder.setSingleChoiceItems(items, selectId, new DialogInterface.OnClickListener() {// 第二個(gè)參數(shù)是設(shè)置默認(rèn)選中哪一項(xiàng)-1代表默認(rèn)都不選 @Override public void onClick(DialogInterface dialog, int which) { tView.setText(items[which]); if (type == 0) { selectWeatherId = which; } else if (type == 1) { selectGrassLandTypeId = which; } else if (type == 2) { selectAgroTypeId = which; } dialog.dismiss(); } }); AlertDialog dialog = builder.create(); dialog.show(); dialog.setCanceledOnTouchOutside(true);// dialog彈出后,點(diǎn)擊界面其他部分dialog消失 }
(2)showMultiDialog()方法,實(shí)現(xiàn)多選效果
boolean[] selected = new boolean[] { false, false };//默認(rèn)選中位置 private void showMultiDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("草地優(yōu)勢(shì)種選擇列表"); DialogInterface.OnMultiChoiceClickListener mutiListener = new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) { selected[which] = isChecked; } }; builder.setMultiChoiceItems(R.array.arr_grasslandGreatType, selected, mutiListener); DialogInterface.OnClickListener btnListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which) { String selectedStr = ""; for (int i = 0; i < selected.length; i++) { if (selected[i] == true) { selectedStr = selectedStr + " " + getResources().getStringArray(R.array.arr_grasslandGreatType)[i]; } } if (!TextUtils.isEmpty(selectedStr)) { tv_grasslandGreatType.setText(selectedStr); } else { tv_grasslandGreatType.setText("暫無(wú)選擇"); } } }; builder.setNegativeButton("取消", null); builder.setPositiveButton("確定", btnListener); AlertDialog dialog = builder.create(); dialog.show(); dialog.setCanceledOnTouchOutside(true);// dialog彈出后,點(diǎn)擊界面其他部分dialog消失 }
以上就是實(shí)現(xiàn)的主要方法。
效果如下:
單選:
多選:
本站還給大家提供了有關(guān)android開(kāi)發(fā)方面的專題欄目,大家可以參考下:
Android 開(kāi)發(fā)中緩存知識(shí)匯總
以上所述是小編給大家介紹的Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對(duì)話框功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能
- Android修改源碼解決Alertdialog觸摸對(duì)話框邊緣消失的問(wèn)題
- Android 自定義AlertDialog對(duì)話框樣式
- Android對(duì)話框AlertDialog.Builder使用方法詳解
- ANDROID中自定義對(duì)話框AlertDialog使用示例
- android自定義AlertDialog對(duì)話框
- Android Alertdialog(實(shí)現(xiàn)警告對(duì)話框)
- Android開(kāi)發(fā)之AlertDialog實(shí)現(xiàn)彈出對(duì)話框
相關(guān)文章
viewpager+photoview實(shí)現(xiàn)圖片查看器
這篇文章主要為大家詳細(xì)介紹了viewpager+photoview實(shí)現(xiàn)圖片查看器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12簡(jiǎn)單介紹Android中Activity的四種啟動(dòng)模式
在Android中每個(gè)界面都是一個(gè)Activity,切換界面操作其實(shí)是多個(gè)不同Activity之間的實(shí)例化操作。本文給大家介紹Android中Activity的四種啟動(dòng)模式,需要的朋友參考下吧2016-04-04Android自定義View實(shí)現(xiàn)比賽時(shí)間閃動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)比賽時(shí)間閃動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Android Activity之間相互調(diào)用與傳遞參數(shù)的原理與用法分析
這篇文章主要介紹了Android Activity之間相互調(diào)用與傳遞參數(shù)的原理與用法,較為詳細(xì)的分析了Android組件的構(gòu)成以及Activity的創(chuàng)建、調(diào)用、切換等相關(guān)操作技巧,需要的朋友可以參考下2016-08-08Android中手機(jī)震動(dòng)的設(shè)置(Vibrator)的步驟簡(jiǎn)要說(shuō)明
Android中手機(jī)震動(dòng)的設(shè)置(Vibrator)的步驟,很詳細(xì),感興趣的朋友可以了解下哦2013-01-01Android實(shí)戰(zhàn)教程第一篇之最簡(jiǎn)單的計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第一篇,如何實(shí)現(xiàn)最簡(jiǎn)單的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android開(kāi)發(fā)中Widget的生命周期實(shí)例分析
這篇文章主要介紹了Android開(kāi)發(fā)中Widget的生命周期,結(jié)合實(shí)例形式分析了Android開(kāi)發(fā)中Widget生命周期所涉及的常用方法與使用技巧,代碼備有詳盡的注釋便于理解,需要的朋友可以參考下2016-02-02一步步實(shí)現(xiàn)Viewpager卡片翻頁(yè)效果
一步步實(shí)現(xiàn)Viewpager卡片翻頁(yè)效果,文章很精彩,實(shí)現(xiàn)步驟很詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08