简体   繁体   中英

check all checkboxes, when one checkbox is clicked in android?

The thing I'm trying is to check all those checkboxes, when "cbkomplet" is checked. But it doesn't work. Any help? Here's the code I have so far.

Thanks in advance.

CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.cbkomplet ); repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if ( isChecked ) { cbreg.isChecked(); cbtank.isChecked(); cbzoop.isChecked(); cbkom.isChecked(); cbmaske.isChecked(); cbbl.isChecked(); } } });

you want to call setChecked(), not isChecked() on the dependent check boxes. you are simply testing if each of them is checked, then throwing away the result.

http://developer.android.com/reference/android/widget/CompoundButton.html#setChecked(boolean )

Use this code.

  chkbxAll = (CheckBox)findViewById(R.id.cbAll);
    chkbxOne = (CheckBox)findViewById(R.id.chkbxOne );
    chkbxTwo = (CheckBox)findViewById(R.id.chkbxTwo );
    chkbxThree = (CheckBox)findViewById(R.id.chkbxThree );
    chkbxFour = (CheckBox)findViewById(R.id.chkbxFour );

 chkbxAll .setOnCheckedChangeListener(new      
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
          isChecked) {
            if (chkbxAll.isChecked()) {
                chkbxOne.setChecked(true);
                chkbxTwo.setChecked(true);
                chkbxThree.setChecked(true);
                chkbxFour.setChecked(true);
            }else {
                chkbxOne.setChecked(false);
                chkbxTwo.setChecked(false);
                chkbxThree.setChecked(false);
                chkbxFour.setChecked(false);
            }
        }
    });

You are calling the isChecked() method, which returns a boolean to tell you if the checkbox is checked. Please use setChecked() instead, and pass in true or false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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