在 Android 的 Multichoice AlertDialog 中设置复选框状态
Set checkboxes status in Multichoice AlertDialog in Android
以下是我的 AlertDialog.Builder
代码
final String[] values = new String[] {"Select All", "Android", "ios", "windows", "Blackberry"};
final ArrayList<String> selecteditems = new ArrayList<String>();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select your favorite OS");
builder.setMultiChoiceItems(values, null , new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selecteditems.add(String.valueOf(which));
} else {
selecteditems.remove(Integer.valueOf(which));
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
我想要做的是能够检测何时检查 'Select All',我可以从 isChecked
循环中检测到。但是我希望能够在用户选中 'Select All' 时将所有选项设置为选中,并在用户取消选中时删除选中 'Select All'.
应该是类似下面的代码。关键是提供一个 boolean[]
的 checkedItems
数组,稍后当您尝试全部 select 时更新它。
checkedItems
数组应该更新(因为对话框仍然引用它。)
和 dialog.getListView().setItemChecked(i, true);
应该为每个项目调用。
items.add(0, "Select All");
boolean[] checkedItems = new boolean[items.size()];
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(dialogTitle)
.setMultiChoiceItems(items.toArray(new String[items.size()]), checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(which == 0) {
if(isChecked) {
multichoiceDialog.getListView().setItemChecked(0, true);
checkedItems[0] = true;
for(int i=1; i< checkedItems.length; i++) {
checkedItems[i] = true;
multichoiceDialog.getListView().setItemChecked(i, true);
}
}
}
}
})
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
multichoiceDialog = builder.create();
multichoiceDialog.show();
以下是我的 AlertDialog.Builder
final String[] values = new String[] {"Select All", "Android", "ios", "windows", "Blackberry"};
final ArrayList<String> selecteditems = new ArrayList<String>();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select your favorite OS");
builder.setMultiChoiceItems(values, null , new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selecteditems.add(String.valueOf(which));
} else {
selecteditems.remove(Integer.valueOf(which));
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
我想要做的是能够检测何时检查 'Select All',我可以从 isChecked
循环中检测到。但是我希望能够在用户选中 'Select All' 时将所有选项设置为选中,并在用户取消选中时删除选中 'Select All'.
应该是类似下面的代码。关键是提供一个 boolean[]
的 checkedItems
数组,稍后当您尝试全部 select 时更新它。
checkedItems
数组应该更新(因为对话框仍然引用它。)和
dialog.getListView().setItemChecked(i, true);
应该为每个项目调用。items.add(0, "Select All"); boolean[] checkedItems = new boolean[items.size()]; AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle(dialogTitle) .setMultiChoiceItems(items.toArray(new String[items.size()]), checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if(which == 0) { if(isChecked) { multichoiceDialog.getListView().setItemChecked(0, true); checkedItems[0] = true; for(int i=1; i< checkedItems.length; i++) { checkedItems[i] = true; multichoiceDialog.getListView().setItemChecked(i, true); } } } } }) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); multichoiceDialog = builder.create(); multichoiceDialog.show();