亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對(duì)話框功能

 更新時(shí)間:2017年03月22日 14:24:39   作者:Mr_zhoujp  
在使用AlertDialog實(shí)現(xiàn)單選和多選對(duì)話框時(shí),分別設(shè)置setSingleChoiceItems()和setMultiChoiceItems()函數(shù)。具體實(shí)現(xiàn)代碼大家參考下本文吧

在使用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 驗(yàn)證碼功能 

Android RecyclerView使用方法匯總   

Android ListView常見(jiàn)功能  

Android控件imageview詳細(xì)用法  

Android SDK基礎(chǔ)教程  

Android 開(kāi)發(fā)中緩存知識(shí)匯總 

以上所述是小編給大家介紹的Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對(duì)話框功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論