String[] items={"餐饮","出行","娱乐","学习","日用品","其他"};
String consumetype="";
List<String> mytypes = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(addmess.this);标签:builder,DialogInterface,复选框,consumetype,mytypes,dialog,按钮,new,android From: https://www.cnblogs.com/jiazq/p/17284890.html
builder.setTitle("请选择消费类型:");
builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
mytypes.clear();
if (isChecked) {
mytypes.add(items[which]);
} else {
mytypes.remove(items[which]);
}
}
});
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
consumetype="";
for (String color : mytypes) {
consumetype+= color + "、";
}
Toast.makeText(getApplicationContext(), "你选择了: " + consumetype, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mytypes.clear();
dialog.dismiss();
}
});
AlertDialog dialog = builder.create(); //创建AlertDialog对象
dialog.show(); //显示对话框 //显示对话框