我有一个对话框与复选框,我正在尝试做不同的事情,当选项被选择时,按好.我以为我知道在阅读一些教程后我在做什么,但是当我按好的时候,它只是烤“一切”,即使没有检查.所以似乎我的if语句不能正常工作,但是我不知道为什么.
任何关于我做错了什么的建议,以及如何解决它将不胜感激!
final CharSequence[] items = {"Item 1", "Item 2", "Item 3"};
final boolean[] states = {false, false, false};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("What would you like to do?");
builder.setMultiChoiceItems(items, states, new DialogInterface.OnMultiChoiceClickListener(){
public void onClick(DialogInterface dialogInterface, int item, boolean state) {
}
});
builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SparseBooleanArray CheCked = ((AlertDialog)dialog).getListView().getCheckedItemPositions();
if(CheCked.get(CheCked.keyAt(0)) == true){
Toast.makeText(Backup.this, "Item 1", Toast.LENGTH_SHORT).show();
}
if(CheCked.get(CheCked.keyAt(1)) == true){
Toast.makeText(Backup.this, "Item 2", Toast.LENGTH_SHORT).show();
}
if(CheCked.get(CheCked.keyAt(2)) == true){
Toast.makeText(Backup.this, "Item 3", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create().show();
}