如何在android中达到限制时禁用复选框?

时间:2013-11-18 05:21:23

标签: android android-checkbox oncheckedchanged

我有15个CheckBox,当我检查超过5时我必须阻止用户。我使用方法OnCheckedChangeListener知道项目是否已被检查,但我不知道如何在选择5个项目后限制

请参阅下面的代码:

int lengthBox = 15;
int lenghtCount = 0;  

// inside onCreate method
final CheckBox[] checkbox = new CheckBox[lengthBox];
OnCheckedChangeListener checker = new OnCheckedChangeListener(){ 
    @Override
    public void onCheckedChanged(CompoundButton cb, boolean b) {
        // How can I improve this condition?
        if(checkbox[0].isChecked() || checkbox[1].isChecked() || 
        checkbox[2].isChecked() || checkbox[3].isChecked() || 
        checkbox[4].isChecked() || checkbox[5].isChecked() || 
        checkbox[6].isChecked() || checkbox[7].isChecked() || 
        checkbox[8].isChecked() || checkbox[9].isChecked() || 
        checkbox[10].isChecked() || checkbox[11].isChecked() || 
        checkbox[12].isChecked() || checkbox[13].isChecked() || 
        checkbox[14].isChecked()) {  
            if(lenghtCount < 5){
                lenghtCount++;
            }else{
                Toast.makeText(MyActivity.this, "Limit reached!!!", Toast.LENGTH_SHORT).show();
            }
        }
    }
};
for(int i = 0; i < lengthBox; i++) {
    int id = getResources().getIdentifier("checkbox_"+i, "id", getPackageName());
    checkbox[i] = (CheckBox) findViewById(id)
    checkbox[i].setOnCheckedChangeListener(checker);
}

有人可以用正确的方式指出我吗?


更新:我试过了:

OnCheckedChangeListener checker = new OnCheckedChangeListener(){ 
    @Override
    public void onCheckedChanged(CompoundButton cb, boolean b) {
        if(lenghtCount == 5){
            cb.setChecked(false);
        }else if(b){
            lenghtCount++;
        }else if(!b){
            lenghtCount--;
        }
    }
};  

完美无缺,但我觉得缺少了一些东西。当我点击5个项目时,这有效,但是当我取消选择它们之后,我无法重新检查任何项目。我错过了什么?

4 个答案:

答案 0 :(得分:8)

你应该使用另一个变量来计算检查的复选框数量

final int count=0; 
OnCheckedChangeListener checker = new OnCheckedChangeListener(){ 
        @Override
        public void onCheckedChanged(CompoundButton cb, boolean b) {
           if(count == maxLimit && b){
               cb.setChecked(false);
               Toast.makeText(getApplicationContext(), 
            "Limit reached!!!", Toast.LENGTH_SHORT).show();
           }else if(b){

                count++;
                CharSequence myCheck = tagsCheck[i].getText();
                Toast.makeText(getApplicationContext(), 
                       myCheck + " checked!", 
                       Toast.LENGTH_SHORT)
                .show();
            }else if(!b){
 count--;
        }
    }; 

答案 1 :(得分:0)

我不确定,但这样的事可能会对你有所帮助。创建一个全局计数变量,每次选中一个复选框时,将计数增加1,一旦计数达到最大值,禁用进一步检查。做这样的事情:

      OnCheckedChangeListener checker = new OnCheckedChangeListener(){ 
        @Override
        public void onCheckedChanged(CompoundButton cb, boolean b) {
           if(checkedCount == maxLimit){
               // set to unchecked and return. there must be some method for that 
               // like : setChecked(0) or some other method.
               return;
           }
           if(.isChecked()){

                checkedCount++;
                CharSequence myCheck = tagsCheck[i].getText();
                Toast.makeText(getApplicationContext(), 
                       myCheck + " checked!", 
                       Toast.LENGTH_SHORT)
                .show();
            }
        }
    };    

答案 2 :(得分:0)

试试这个..创建int check = 0;为全局

OnCheckedChangeListener checker = new OnCheckedChangeListener(){ 
    @Override
    public void onCheckedChanged(CompoundButton cb, boolean b) {
   if(isChecked)
    {
        check ++;
        Toast.makeText(getApplicationContext(), 
                   myCheck + " checked! " +buttonView.getText(), 
                   Toast.LENGTH_SHORT)
            .show();            
    }
    else
    {
        check --;
        Toast.makeText(getApplicationContext(), 
                   myCheck + "Un checked! " +buttonView.getText(), 
                   Toast.LENGTH_SHORT)
            .show();    

    }


    if(check >= 5)
    {
        Toast.makeText(getApplicationContext(), 
                   myCheck + "Checked 5", 
                   Toast.LENGTH_SHORT)
            .show();
    }   
    }
};  

答案 3 :(得分:0)

假设您必须从4个中选择最多2个复选框,那么以下代码很有用:-

    Checkbox[] CBA = new CheckBox[4];
    int Count=0;
    CBA[0]=findViewByID(R.id.C1);
    CBA[1]=findViewByID(R.id.C2);
    CBA[2]=findViewByID(R.id.C3);
    CBA[3]=findViewByID(R.id.C4);
    CBA[0].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    for(int i=0;i<CBA.length;i++)
        if(CBA[i].isChecked)
        {
         Count++;
        } 
    if(Count>2)
    {
     CBA[0].setCheck(false);
    }                       
    }

);

对另外3个复选框执行相同操作 您也可以通过对整个数组使用Checkchanged侦听器来执行此操作(CBA [i] .setOnCheckedChangeListener)............希望它将很有用