2016-08-20 69 views
0

我給陣列選擇通過警告對話框我想禁用選擇多個選項,如果數組滿禁用複選框項目在alertDialog

我已經能夠停止添加項目陣列到達極限,但不能後禁用複選框

AlertDialog.Builder builder=new AlertDialog.Builder(this); 
View view=inflater.inflate(R.layout.dialog_genre,null,false); 
builder.setView(view).setTitle("Select Genres"); 

builder.setMultiChoiceItems(array, checkedGenres, new DialogInterface.OnMultiChoiceClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
      if(isChecked) { 
       if (!selectedgenre.contains(String.valueOf(which))) 
        if(selectedgenre.size()<5) 
       { 
        selectedgenre.add(String.valueOf(which)); 
        checkedGenres[which]=true; 
       } 
       else{ 
         Toast.makeText(getApplicationContext(),"you can't add more genres",Toast.LENGTH_LONG).show(); 
        } 
      } 
      else if (selectedgenre.contains(String.valueOf(which))) 
      { 
      selectedgenre.remove(String.valueOf(which)); 
      checkedGenres[which]=false; 
     } 
    } 
} 

enter image description here

回答

0

好吧,我已經解決了這個問題,兩天後我才知道禁用複選框漫長而忙碌的過程,但我們可以禁止的選擇,這就是我想做的事。這裏是我的解決方案

builder.setMultiChoiceItems(array, checkedGenres, new DialogInterface.OnMultiChoiceClickListener() { 
     int count = 0; 
     @Override 
     public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
      if(isChecked) { 
       if (!selectedgenre.contains(String.valueOf(array[which]))) 
        if(selectedgenre.size()<5) 
       { 
        selectedgenre.add(String.valueOf(array[which])); 
        checkedGenres[which]=true;      
       } 
       else{ 
         count--; 
         ((AlertDialog) dialog).getListView().setItemChecked(which, false); 
         checkedGenres[which]=false; 
         Toast.makeText(getApplicationContext(),"you can't add this genre",Toast.LENGTH_LONG).show(); 
        } 
      } 
      else if (selectedgenre.contains(String.valueOf(array[which]))) 
      { 
       selectedgenre.remove(String.valueOf(array[which])); 
       checkedGenres[which]=false; 
      } 
     } 
    } 
0
builder.setMultiChoiceItems(array, checkedGenres, new DialogInterface.OnMultiChoiceClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
       if(isChecked) { 
        if (!selectedgenre.contains(String.valueOf(which))) 
         if(selectedgenre.size()<5) 
        { 
         selectedgenre.add(String.valueOf(which)); 
         checkedGenres[which]=true; 
        } 
        else{ 
         //set your checkbox to false here 
         //yourCheckbox.setchecked(false); 
          Toast.makeText(getApplicationContext(),"you can't add more genres",Toast.LENGTH_LONG).show(); 
         } 
       } 
       else 
       { 
       selectedgenre.remove(String.valueOf(which)); 
       checkedGenres[which]=false; 
      } 
     } 
    } 

這一嘗試..問題在你的其他部分

+0

我要禁用的選擇,即別人的'如果(selectedgenre.size()<5)' – rookieDeveloper

+0

沒有得到你想要做的.. –

+0

如果我的計數達到什麼5我要禁用複選框,用戶應該能夠選擇,你可以看到圖像甚至達到5被允許選擇 – rookieDeveloper

0

在您的其他部分,在您的複選框上放置可點擊的false,並更改顏色以便用戶識別它。

+0

我該怎麼做之後? – rookieDeveloper