android alertdialog 实现单选 多选对话框 Android使用AlertDialog实现的信息列表单选、多选对话框功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

android alertdialog 实现单选 多选对话框 Android使用AlertDialog实现的信息列表单选、多选对话框功能

Mr_zhoujp   2021-03-24 我要评论
想了解Android使用AlertDialog实现的信息列表单选、多选对话框功能的相关内容吗,Mr_zhoujp在本文为您仔细讲解android alertdialog 实现单选 多选对话框的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:alertdialog单选,alertdialog多选,android,alertdialog,下面大家一起来学习吧。

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

下面看主要的代码:

数据源数组:

<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:// 草地优势种选择
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开发方面的专题栏目,大家可以参考下:

android 验证码功能 

Android RecyclerView使用方法汇总   

Android ListView常见功能  

Android控件imageview详细用法  

Android SDK基础教程  

Android 开发中缓存知识汇总 

以上所述是小编给大家介绍的Android使用AlertDialog实现的信息列表单选、多选对话框功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们