【问题标题】:How to get all selected indexes in AlertDialog multiChoiceItems如何在 AlertDialog multiChoiceItems 中获取所有选定的索引
【发布时间】:2011-07-27 16:10:46
【问题描述】:
我在试图弄清楚这一点时遇到了一些麻烦。我所拥有的是一个多选警报对话框,我想要做的是当按下肯定按钮时,我希望在检查的索引上执行一项任务。我该怎么做呢?
这就是我的目标......
dialog.setMultiChoiceItems(list, null, null);
dialog.setPositiveButton("Okay", null);
摘要:如何从 AlertDialog 中获取所有已检查的索引?
【问题讨论】:
标签:
android
list
listener
android-alertdialog
【解决方案1】:
我采用的方法是声明一个final Boolean [] 来存储项目的状态,然后当我调用setMultiChoiceItems 方法时,我提供了一个DialogInterface.OnMultiChoiceClickListener,它设置了这个数组中每个项目的状态,当它改变时.然后当点击正面按钮时,我可以从DialogInterface.OnClickListener 引用这个数组。
例如(从我的一些代码中复制并稍微混淆):
final int aIndex = 0;
final int bIndex = 1;
final int cIndex = 2;
final int dIndex = 3;
final CharSequence[] items = {
context.getString(R.string.string_share_include_a),
context.getString(R.string.string_share_include_b),
context.getString(R.string.string_share_include_c),
context.getString(R.string.string_share_include_d) };
final Boolean[] state = new Boolean[4];
state[aIndex] = true;
state[bIndex] = true;
state[cIndex] = true;
state[dIndex] = false;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.string_share_dialog_title);
builder.setMultiChoiceItems(items, new boolean[] { true, true, true,
false }, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
state[which] = isChecked;
}
});
builder.setPositiveButton(R.string.string_share_ok,
new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Utilities.shareStuff(
state[aIndex],
state[bIndex],
state[cIndex],
state[dIndex]);
}
});