我想用一个监听器做一个多选项对话框,按顺序我按了选项。我怎么能这样做?
String[] multiChoiceItems =
getResources().getStringArray(R.array.dialog_choice_array_monday);
boolean[] checkedItems = {false, false, false, false, false, false, false};
new AlertDialog.Builder(NewScheduleActivity.this)
.setTitle(getString(R.string.main_dialog_multi_choice_monday))
.setMultiChoiceItems(multiChoiceItems, checkedItems, null)
.setPositiveButton(getString(R.string.dialog_ok), null)
.setNegativeButton(getString(R.string.dialog_cancel), null)
.show();
}
答案 0 :(得分:0)
这应该有所帮助:
.setMultiChoiceItems(multiChoiceItems, null, new DialogInterface.OnMultiChoiceClickListener() {
});
当你开始输入新的DialogeInterface.OnMul ......它将自动实现抽象方法:
onClick(DialogInterface dialog, int which, boolean isChecked)
我认为您可以使用null而不是checkedItems数组,具体取决于您是否需要使用此数组中的数据。
您需要做什么:
为序列创建一个新数组,在onClick Listener中,只需添加内部代码即可在sequenceArray中添加所选元素!最后,此数组将按其选择的顺序包含数据。
String[] multiChoiceItems = getResources().getStringArray(R.array.dialog_choice_array_monday);
ArrayList<String> sequenceArray = new ArrayList<>();
new AlertDialog.Builder(NewScheduleActivity.this)
.setTitle(getString(R.string.main_dialog_multi_choice_monday))
.setMultiChoiceItems(multiChoiceItems, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
sequenceArray.add(multiChoiceItems[which])
}
}
.setPositiveButton(getString(R.string.dialog_ok), null)
.setNegativeButton(getString(R.string.dialog_cancel), null)
.show();
}
Android网站:https://developer.android.com/reference/android/content/DialogInterface.html