jeudi 14 janvier 2016

CheckBox doesnt reponse to check state (Android Studio)

Lets suppose I have 2 checkbox ("checkbox1" and "checkbox2") and I set: SetChecked(true) For "checkbox1" and : SetChcked(false) for "checkbox2". my purpose is to relate them and switch between the check state when I click on one of them ,(if "checkbox1" is checked and I checked "checkbox2" then "checkbox2" will be checked and "checkbox1" will be unchecked and by viceversa)
Here is my code :

final CheckBox ChckBoxNo = (CheckBox) promptsView.findViewById(R.id.ChkBoxNo);
final CheckBox ChckBoxYes = (CheckBox) promptsView.findViewById(R.id.ChkBoxYes);
ChckBoxNo.setChecked(true);
ChckBoxYes.setChecked(false);

ChckBoxNo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (ChckBoxNo.isChecked()) {
            ChckBoxYes.setChecked(false);
        } else if (!ChckBoxNo.isChecked()) {
            ChckBoxYes.setChecked(true);
        }

    }

});

ChckBoxYes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (ChckBoxYes.isChecked()) {
            ChckBoxNo.setChecked(false);
        } else if (!ChckBoxYes.isChecked()) {
            ChckBoxNo.setChecked(true);
        }

    }

});

   //these if's allways response to "ChckBoxNo.IsChecked()" ,even if its the 
     // opposite and "ChckBoxYes.isChcked()"          


   if(ChckBoxYes.isChecked())
   {do ...
         }
    else if (ChckBoxNo.isChecked()
      { do ...
               }

When I run the app by visibility evreything is good When I click on "ChckBoxYes" it's checked and "ChckboxNo" is unchecked and viceversa,but its allways get the default value that I set before implenting the "OnCheckedChangeListener" (ChckBoxNo.SetChcked(true), ChckBoxYes.SetChecked(false) , even if its opposite and "ChckBoxYes" is chcked) What should I do in order to fix that? thanks !




Aucun commentaire:

Enregistrer un commentaire