Skip to content Skip to sidebar Skip to footer

How Do I Get My Checkbox To Do Something When It's Checked?

For example, I have 5 checkboxes (cb1 - cb5). cb1 is superior to others, which means it can only be checked if all the other 4 are checked. And if I check cb1, all the other 4 shou

Solution 1:

Create a class for checkbox listener

publicclassCheckBoxListenerimplementsOnCheckedChangeListener {

    publicCheckBoxListener() {

    }


    @OverridepublicvoidonCheckedChanged(CompoundButton view, boolean isChecked) {
        switch(view.getId()) {
        case R.id.checkBox1: 
            //do somethingcase R.id.checkBox2:

        case ...
        }

    }
}

Then set this listener to all your checkboxes:

finalCheckBoxcb1= (CheckBox) findViewById(R.id.checkBox1);
finalCheckBoxcb2= (CheckBox) findViewById(R.id.checkBox2);
finalCheckBoxcb3= (CheckBox) findViewById(R.id.checkBox3);
finalCheckBoxcb4= (CheckBox) findViewById(R.id.checkBox4);
finalCheckBoxcb5= (CheckBox) findViewById(R.id.checkBox5);
cb1.setOnCheckedChangeListener(newCheckBoxListener());
cb2.setOnCheckedChangeListener(newCheckBoxListener());
cb3.setOnCheckedChangeListener(newCheckBoxListener());
cb4.setOnCheckedChangeListener(newCheckBoxListener());

Post a Comment for "How Do I Get My Checkbox To Do Something When It's Checked?"