lundi 2 novembre 2015

Android: How to uncheck checkbox without perform click on it?

I have 3 checkbox in my Layout:

  • (case #1) mCheckBoxAll: when is checked or unchecked, all other boxes are set in the same state.
  • (case #2) mCheckBoxChoice1 and mCheckBoxChoice2: if one of these is unchecked by the user then the mCheckBoxAll must be unchecked.

    mCheckBoxAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mCheckBoxChoice1.setChecked(isChecked);
                    mCheckBoxChoice2.setChecked(isChecked);
                }
            });
    
    mCheckBoxChoice1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!isChecked)
                         mCheckBoxAll.setChecked(!isChecked);
                }
            });
    
    mCheckBoxChoice2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!isChecked)
                         mCheckBoxAll.setChecked(!isChecked);
                }
            });
    
    

But this code doesn't work in case #2. All cases are unchecked in the same time...




Aucun commentaire:

Enregistrer un commentaire