三、多选对话框 核心代码:
public void click3(View view) {
AlertDialog.Builder builder = new Builder(this);// 这里不能用getApplicationContext(),只能用this
builder.setTitle("您喜欢吃什么?");
// builder.setMessage("不选择就默认吃粑粑,hiahia!!!");//这句不能加上,否则显示不出来多选对话框
// 1、多选对话框
final CharSequence[] items = { "小米", "大米", "玉米", "屎粑粑" };
boolean[] checkedItems = { false, false, false, false, true };
builder.setMultiChoiceItems(items, checkedItems,new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "您选择了:" + items[which],Toast.LENGTH_SHORT).show();
}
}
});
// 2、确定按钮
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();// 关闭对话框
}
});
// 3、取消按钮
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();// 关闭对话框
}
});
builder.show();
} 结果: |