Android使用AlertDialog實現(xiàn)的信息列表單選、多選對話框功能
在使用AlertDialog實現(xiàn)單選和多選對話框時,分別設(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中的主要代碼:
點擊事件:
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)勢種選擇 showMultiDialog(); break;
對應(yīng)方法:
(1)showAlertDialog()方法,實現(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() {// 第二個參數(shù)是設(shè)置默認(rèn)選中哪一項-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彈出后,點擊界面其他部分dialog消失
}
(2)showMultiDialog()方法,實現(xiàn)多選效果
boolean[] selected = new boolean[] { false, false };//默認(rèn)選中位置
private void showMultiDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("草地優(yōu)勢種選擇列表");
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("暫無選擇");
}
}
};
builder.setNegativeButton("取消", null);
builder.setPositiveButton("確定", btnListener);
AlertDialog dialog = builder.create();
dialog.show();
dialog.setCanceledOnTouchOutside(true);// dialog彈出后,點擊界面其他部分dialog消失
}
以上就是實現(xiàn)的主要方法。
效果如下:
單選:

多選:

本站還給大家提供了有關(guān)android開發(fā)方面的專題欄目,大家可以參考下:
以上所述是小編給大家介紹的Android使用AlertDialog實現(xiàn)的信息列表單選、多選對話框功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android實現(xiàn)點擊AlertDialog上按鈕時不關(guān)閉對話框的方法
- Android中AlertDialog各種對話框的用法實例詳解
- Android中AlertDialog 點擊按鈕后不關(guān)閉對話框的功能
- Android修改源碼解決Alertdialog觸摸對話框邊緣消失的問題
- Android 自定義AlertDialog對話框樣式
- Android對話框AlertDialog.Builder使用方法詳解
- ANDROID中自定義對話框AlertDialog使用示例
- android自定義AlertDialog對話框
- Android Alertdialog(實現(xiàn)警告對話框)
- Android開發(fā)之AlertDialog實現(xiàn)彈出對話框
相關(guān)文章
android開發(fā)之方形圓角listview代碼分享
我寫這篇文章受到了kiritor的專欄發(fā)表的博文Android UI控件之ListView實現(xiàn)圓角效果的啟發(fā)。2013-06-06
Android?Compose之Animatable動畫停止使用詳解
這篇文章主要為大家介紹了Android?Compose之Animatable動畫停止使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理
今天小編就為大家分享一篇關(guān)于CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Textvie實現(xiàn)左邊圖片和換行文字左對齊的方法
下面小編就為大家分享一篇Textvie實現(xiàn)左邊圖片和換行文字左對齊的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android網(wǎng)絡(luò)編程之UDP通信模型實例
這篇文章主要介紹了Android網(wǎng)絡(luò)編程之UDP通信模型實例,本文給出了服務(wù)端代碼和客戶端代碼,需要的朋友可以參考下2014-10-10

