网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在Android中利用AlertDialog实现一个多选框功能-创新互联

怎么在Android中利用AlertDialog实现一个多选框功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联主营蟠龙网站建设的网络公司,主营网站建设方案,app软件开发,蟠龙h5小程序开发搭建,蟠龙网站营销推广欢迎蟠龙等地区企业咨询

在使用AlertDialog实现单选和多选对话框时,分别设置setSingleChoiceItems()和setMultiChoiceItems()函数。

数据源数组:




  晴
  多云
  小雨
  中雨
 


  羊草
  牛草
 

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:// 草地优势种选择
showMultiDialog();
break;

对应方法:

(1)showAlertDialog()方法,实现单选效果,selectWeatherId 设置选定的条目位置

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() {// 第二个参数是设置默认选中哪一项-1代表默认都不选
@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()方法,实现多选效果

boolean[] selected = new boolean[] { false, false };//默认选中位置
private void showMultiDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("草地优势种选择列表");
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消失
}

看完上述内容,你们掌握怎么在Android中利用AlertDialog实现一个多选框功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


名称栏目:怎么在Android中利用AlertDialog实现一个多选框功能-创新互联
文章转载:http://bjjierui.cn/article/cophii.html

其他资讯