简体   繁体   English

Android Checkbox启用和禁用

[英]Android Checkbox enable and disable

I have question, 我有问题,

I have checkbox,that are cb1,cb2,cb3,cb4. 我有一个复选框,分别是cb1,cb2,cb3,cb4。 I want, when i checked cb1 then cb2 will be disable and cb3,cb4 keep enable, after that i checked again cb3 then cb4 will be disable too. 我想要,当我检查cb1时,cb2将被禁用,而cb3,cb4保持启用,此后,我再次检查cb3,则cb4也将被禁用。

The 1st step it worked well, but when i try to checked cb3, then cb4 wont be disable. 第一步工作得很好,但是当我尝试检查cb3时,cb4将不会被禁用。 there is my code 有我的代码

public void onCheckboxClicked(View v){

    CheckBox cb1= (CheckBox)findViewById(R.id.cb1);
    CheckBox cb2= (CheckBox)findViewById(R.id.cb2);
    CheckBox cb3= (CheckBox)findViewById(R.id.cb3);
    CheckBox cb4= (CheckBox)findViewById(R.id.cb4);


    switch (v.getId()) {
    case R.id.cb1:
        if (cb1.isChecked()) {
            cb2.setEnabled(false);

        }else {
            cb2.setEnabled(true);


        }

        break;

    //......

please help me guys!! 请帮助我!

Update Question: 更新问题:

what i mean, something like this,I have a 10 checkboxes, when I was checking one of them, then the number of checkboxes that can be checked turned into 5, then when one of these 5 checkbox is checked, then it will be reduced again to 2 checkbox where is enable to be checked, will be like that until there are no more checkboxes can be checked 我的意思是,这样,我有10个复选框,当我选中其中一个复选框时,可以检查的复选框数量变成5,然后当选中这5个复选框之一时,它将减少再次到2复选框(启用复选框),直到没有更多的复选框可以被选中为止

Try to do like this 尝试这样做

switch (v.getId()) {
case R.id.cb1:
    if (cb1.isChecked()) {
        cb2.setChecked(false);
    }else {
        cb2.setChecked(true);


    }

    break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM