How to disable checkbox when a limit is reached in android?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 21:37:27
zohreh

you should use a other variable for counting number of checkboxs that checked

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--;
        }
    }; 

I am not sure but something like this might help you. create a global count variable and every time a checkbox is checked, increase count by 1 and once count reaches max, disable further checks. Do something like this :

      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();
            }
        }
    };    

Try this.. Create int check = 0; as global

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();
    }   
    }
};  

Suppose You Have To Select Maximum 2 CheckBoxes Out OF 4 Then Following Code Is Useful:-

    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);
    }                       
    }

);

Do Same for Another 3 Checkbox You Can Also Do This By Using Checkchanged Listener To Whole Array(CBA[i].setOnCheckedChangeListener)............Hope It Will Be Useful

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!