如何取消选中radiogroup中的单选按钮并听取操作

时间:2018-03-03 09:09:04

标签: java android checkbox radio-button radio-group

我有3个自定义单选按钮,默认情况下不会检查它们。它们就像一个过滤器。我想知道是否有办法取消选中已选中的按钮并听取该操作。所以基本上我可以取消过滤器。

我知道在这种情况下我应该使用checkbox,但我不想要检查不止一个!!

 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;
        }
      }
    });

}

3 个答案:

答案 0 :(得分:0)

试试这段代码,希望它能帮到你..

 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;
        }
      }
    });

}

答案 1 :(得分:0)

似乎单选按钮不起作用,所以我用复选框替换它们并手动处理它们。它工作了。!

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();
    }
  }
});

答案 2 :(得分:0)

你应该有hideRedCards()hideBlueCard()hideGreenCards()方法来隐藏你的卡片,当radioButton是unChecked时,请调用此方法

    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();
                    }

        }
    });

}