Android实现单选与多选对话框的代码
android开发中实现单选与多选对话框的代码非常简单,具体代码如下所示:
public void myClick(View view) {
// 单选对话框
//singleCheckDialog();
// 多选对话框
mulCheckDialog();
}
private void mulCheckDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题")
.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder reuslt = new StringBuilder();
for (int i = 0; i < items.length; i++) {
if (checkedItems[i]) {
reuslt.append(items[i]).append(" ");
}
}
Toast.makeText(MainActivity.this, "选择了" + reuslt.toString(), Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
private void singleCheckDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题")
.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String itemStr = items[which];
Toast.makeText(MainActivity.this, "选择了" + itemStr, Toast.LENGTH_SHORT).show();
// 关闭对话框
//dialog.dismiss();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
以上所述是小编给大家介绍的Android实现单选与多选对话框的代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
以上是 Android实现单选与多选对话框的代码 的全部内容, 来源链接: utcz.com/z/356943.html