2010-10-30 120 views
2

我有一个显示复选框列表的对话框。每次显示对话框时,我想设置不同的框。但是这只能在第一次使用。我希望每次显示对话框时都能使用它!这将是巨大的,如果任何人都可以帮助...为复选框列表对话框设置复选框

这是我的代码:

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 

      case CHECKBOX_LIST_DIALOG: 

       final CharSequence[] weeks = new CharSequence[53]; 

       for (int i=0; i<=52; i++) { 
        weeks[i] = String.valueOf(i+1); 
       } 

       return new AlertDialog.Builder(this).setTitle(
         R.string.txt_week_checkbox_list).setMultiChoiceItems(
         weeks, getCheckedBoxes(), 
         new DialogInterface.OnMultiChoiceClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) { 
           checked[whichButton] = isChecked; 
          } 
         }).setPositiveButton("OK", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
           EditText editText = (EditText) findViewById(R.id.edittext_weeks); 
           editText.setText(generateString()); 
          } 
         }).setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int whichButton) { 
          } 
         }).create(); 
} 

回答

3

通过onCreateDialog()创建的托管对话框缓存。您将需要覆盖onPrepareDialog(),以便在下次显示对话框时获得控制权。您通过Dialog对象。将其转换为AlertDialog,请致电getListView(),并使用setItemChecked()开启或关闭每个复选框。

0

太棒了!这样做,谢谢!这正是我正在寻找的:-) 下面是我所做的工作,就像你解释的那样:

@Override 
protected void onPrepareDialog(int id, Dialog dialog) { 
    ListView lv = ((AlertDialog) dialog).getListView(); 
    boolean[] checked = myDialog.getCheckedBoxes(); 
    for (int i=0; i<checked.length; i++) 
     if (checked[i]) 
      lv.setItemChecked(i, true); 
}