我採取的方法是宣佈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]);
}
});
這正是即時尋找!謝謝! – thunderousNinja