android studio 单选对话框或多选对话框的实现
时间: 2023-10-31 08:06:00 浏览: 225
要在Android Studio中实现单选对话框或多选对话框,可以使用AlertDialog.Builder类。下面是实现单选对话框和多选对话框的代码示例:
单选对话框:
```java
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("选择项");
builder.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 处理选中项的逻辑
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 处理确定按钮点击事件的逻辑
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 处理取消按钮点击事件的逻辑
}
});
AlertDialog dialog = builder.create();
dialog.show();
```
多选对话框:
```java
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("选择项");
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// 处理选中项的逻辑
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 处理确定按钮点击事件的逻辑
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 处理取消按钮点击事件的逻辑
}
});
AlertDialog dialog = builder.create();
dialog.show();
```
阅读全文