Zoo*_*key 0 android android-layout android-dialog android-checkbox
我正在使用带有多项选择的 AlertDialog 来显示可检查项目的列表。
当用户选择某些值时,我可以获取它们的索引并将其保存到列表中。效果很好。
但我希望当用户再次打开 AlertDialog 时选择/检查他之前选择的值。
这是代码:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMultiChoiceItems(R.array.array_cousine, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int indexSelected,
boolean isChecked) {
if (isChecked) {
seletedItems.add(++indexSelected);
} else if (seletedItems.contains(indexSelected)) {
seletedItems.remove(Integer.valueOf(++indexSelected));
}
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String[] expList = getResources().getStringArray(R.array.array_cousine);
for (int i = 0; i < seletedItems.size(); i++) {
int selected = seletedItems.get(i);
String selectedString = expList[selected - 1];
selectedItemsName.add(selectedString);
}
StringBuilder stringBuilder = new StringBuilder();
for (int j = 0; j < selectedItemsName.size(); j++) {
String text = selectedItemsName.get(j);
stringBuilder = stringBuilder.append(" "+text);
}
Log.d("TAG", "String builder: " + stringBuilder);
tvCusine.setText(stringBuilder);
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
Dialog dialog = builder.create();//AlertDialog dialog;
dialog.show();
Run Code Online (Sandbox Code Playgroud)
这是图片:
其中的第二个参数是您当前作为 null 传入的builder.setMultiChoiceItemsa 。boolean[]要在打开时将项目显示为已选中,请将此数组传递到true要检查的每个项目的位置。这些值可以在创建数组后设置boolean[position] = value
| 归档时间: |
|
| 查看次数: |
3202 次 |
| 最近记录: |