2011-02-03 69 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; 
     } 
相關問題