简体   繁体   中英

RadioButton not deselecting

Here is my code, I want to unselect a radio button when I click on it again. Since I have some code that only works when none of the radiobuttons are selected.

    if (TablesOn == true) {
        TablesOn = false;
    } else {
        TablesOn = true;
    }

    if (jRadioButton1.isVisible()==true) {
      jRadioButton1.setVisible(false);
      jRadioButton2.setVisible(false);
      jRadioButton3.setVisible(false);
      jLabel3.setVisible(false);
      jLabel4.setVisible(false);
      jLabel5.setVisible(false);
    } else {
      jRadioButton1.setVisible(true);
      jRadioButton2.setVisible(true);
      jRadioButton3.setVisible(true);
      jLabel3.setVisible(true);
      jLabel4.setVisible(true);
      jLabel5.setVisible(true);
    }

To deselect a RadioButton which is inside of RadioGroup and be able to then select it again, firstly get your RadioGroup :

RadioGroup rg = (RadioGroup) findViewById(R.id.your_radio_group_name_in_layout);

And then do:

rg.check(-1);

Of course, you need some code to save last checked button number, and if it is pressed again, deselect it using the code above.

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