2011-07-27 33 views
3

我在嘗試解決這個問題時遇到了一些麻煩。我擁有的是一個multichoice警報對話框,我想要做的是當按下正向按鈕時,我想要在檢查的索引上執行任務。我該怎麼做呢?如何獲取AlertDialog中的所有選定索引multiChoiceItems

這是我最多...

dialog.setMultiChoiceItems(list, null, null); 
dialog.setPositiveButton("Okay", null); 

摘要:我怎樣才能全部從AlertDialog檢查的指標?

回答

8

我採取的方法是宣佈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]); 
       } 
      }); 
+0

這正是即時尋找!謝謝! – thunderousNinja

相關問題