简体   繁体   中英

How to uncheck a radio button in radiogroup and listen to the action

I have 3 custom radio buttons and they're not checked by default. They act like a filter. I want to know if there's a way to uncheck a checked button and listen to that action. So basically I can cancel the filter.

I know I should use checkbox in this case, but I don't want more than one to be checked!!

 RadioGroup radioGroup;
 RadioButton radioYellow;
 RadioButton radioGreen;
 RadioButton radioRed;
 boolean isChecked;

void handleRadioButtons(){

    radioRed.setChecked(false);
    radioGreen.setChecked(false);
    radioYellow.setChecked(false);
    showAllCards();

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
       // RadioButton rad = group.findViewById(checkedId);
        Log.i("isChecked: ", isChecked + "");

       //I want to call showAllCards() when no button is checked

        switch (checkedId) {
          case R.id.radioRed:
            showRedCards();
            break;

          case R.id.radioGreen:
            showGreenCards();
            break;

          case R.id.radioYellow:
            showYellowCards();
            break;
        }
      }
    });

}

It seems radio button won't work, so I replaced them all with checkboxes and handle them manually. and it worked.!

greenCheckBox.setChecked(false);
yellowCheckBox.setChecked(false);
redCheckBox.setChecked(false);
showAllCards();


greenCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(CompoundButton button, boolean isChecked) {
    if (isChecked){
      yellowCheckBox.setChecked(false);
      redCheckBox.setChecked(false);
      showGreenCards();
    }else {
      showAllCards();
    }
  }
});


yellowCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(CompoundButton button, boolean isChecked) {
    if (isChecked){
      greenCheckBox.setChecked(false);
      redCheckBox.setChecked(false);
      showYellowCards();
    }else {
      showAllCards();
    }
  }
});

redCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(CompoundButton button, boolean isChecked) {
    if (isChecked){
      yellowCheckBox.setChecked(false);
      greenCheckBox.setChecked(false);
      showRedCards();
    }else {
      showAllCards();
    }
  }
});

Try this code, hope it will help you..

 RadioGroup radioGroup;
 RadioButton radioYellow;
 RadioButton radioGreen;
 RadioButton radioRed;
 boolean isChecked;

void handleRadioButtons(){

    radioRed.setChecked(false);
    radioGreen.setChecked(false);
    radioYellow.setChecked(false);
    showAllCards();

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
       // RadioButton rad = group.findViewById(checkedId);
        Log.i("isChecked: ", isChecked + "");

       //I want to call showAllCards() when no button is checked

        switch (checkedId) {
          case R.id.radioRed:
            // check if it is checked or not...

            if(radioRed.isChecked())
                radioRed.setChecked(false);
            else
                radioRed.setChecked(true);

            // showRedCards();
            break;

          case R.id.radioGreen:

            if(radioGreen.isChecked())
                radioGreen.setChecked(false);
            else
                radioGreen.setChecked(true);

            //showGreenCards();
            break;

          case R.id.radioYellow:

            if(radioYellow.isChecked())
                radioYellow.setChecked(false);
            else
                radioYellow.setChecked(true);

            //showYellowCards();
            break;
        }
      }
    });

}

You should have a hideRedCards() and hideBlueCard() , hideGreenCards() method that hides your cards, and when the radioButton is unChecked, call this method

    void handleRadioButtons() {

    radioRed.setChecked(false);
    radioGreen.setChecked(false);
    radioYellow.setChecked(false);
    // showAllCards();

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // RadioButton rad = group.findViewById(checkedId);
            Log.i("isChecked: ", isChecked + "");


                    if (radioRed.isChecked()) {
                        radioRed.setChecked(true);
                        showRedCards();

                    } else{
                        radioRed.setChecked(false);
                        HideRedCards();
                    }


                    if (radioGreen.isChecked()) {
                        radioGreen.setChecked(true);
                        showGreenCards();

                    } else{
                        radioGreen.setChecked(false);
                        HideGreenCards();
                    }


                    if (radioYellow.isChecked()) {
                        radioYellow.setChecked(true);
                        showYellowCards();

                    } else{
                        radioYellow.setChecked(false);
                        HideYellowCards();
                    }

        }
    });

}

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