2011-02-03 56 views
0

好吧......我需要用3个复选框创建一个Alert对话框。如果顶部复选框被点击,另外2个应该被点击并禁用!我做了他们点击,但没有禁用。我不知道该怎么做。Android:如何禁用AlertDialog中的CheckBox?

@Override 
protected Dialog onCreateDialog (int id) { 
AlertDialog.Builder builder = new AlertDialog.Builder(this);   
builder.setTitle("AA");    
builder.setMultiChoiceItems(mStrings, mCheckedItems, new DialogInterface.OnMultiChoiceClickListener() {      
public void onClick(final DialogInterface dialog, int which, boolean isChecked) { 


         switch (which) { 
         case 0: { 

         if(isChecked==true) { 
          for (int i = 1; i<=2; i++) {          
          ((AlertDialog) dialog).getListView().setItemChecked(i, true);       
          } 
         } 

         if (isChecked==false) { 
          for (int i = 1; i<=2; i++) {          
           ((AlertDialog) dialog).getListView().setItemChecked(i, false);       
           } 

          break; 
         } 

而这种解决方案并不好。有时候它不会点击所有的复选框。有没有人有任何想法?

回答

3

您应该可以在您要在onClick()侦听器中禁用的两个复选框上调用.setEnabled(false)。出于好奇,你为什么使用for循环结构循环2个项目并将它们设置为检查。在我看来,在两个连续的调用中调用.setChecked()将简化这个过程。

代码示例:

//This line has to go after your dialog.show(); call 
    CheckBox chkBox = (CheckBox) dialog.findViewById(R.id.yourCheckBox); 
//This line will go in your OnClickListener. 
    chkBox.setEnabled(false); 
+0

然后告诉我...如何的setEnabled(假)2等在我的鳕鱼复选框?你可以批准一个鳕鱼吗? – Jim 2011-02-04 08:13:43

0

使mCheckeditems [i] = FALSE如果u想要的复选框选中,反之亦然

0
/* Please set appropriate boolean value in the boolean array which you have 
passed as paramater for 
builder.setMultiChoiceItems(StringArray,BooleanArray, Listener) 
in order to check or uncheck items in dialog */ 

@Override 
protected Dialog onCreateDialog (int id) { 

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

builder.setTitle("AA"); 

builder.setMultiChoiceItems(mStrings, mCheckedItems, 
     DialogInterface.OnMultiChoiceClickListener() { 
    public void onClick(final DialogInterface dialog, int which, boolean isChecked) { 

     switch (which) { 
      case 0: { 
       if(isChecked) { 
        for (int i = 1; i<=2; i++) { 
         mCheckedItems[i] =false; 
        } 
       } else { 
        for (int i = 1; i<=2; i++) { 
         ((AlertDialog) dialog).getListView().setItemChecked(i,false); 
         mCheckedItems[i] =false; 
        } 
       } 
       break; 
     }