Uncheck

The application is an application for a step sequencer with 16 groups of radio stations with 8 buttons in each group. It works great, unless the group has a selected button. I can’t turn it off if I don’t use the clear button that I created to clear all the radio groups. What I would like to add is a code that says that when the selected switch is selected, it just turns off like a switch. I tried using switches, but then other problems arose with this method. Below is an attempt, but it is far from the sign Im guessing

final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup1); RadioButton lC1 = (RadioButton)findViewById(R.id.RadioButtonlowC1); Button D1 = (Button)findViewById(R.id.RadioButtonD1); D1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PdBase.sendFloat("D1", 74); int selectedTypeId = radioGroup1.getCheckedRadioButtonId(); RadioButton D1 = (RadioButton) findViewById(selectedTypeId); if(radioGroup1 != null) // This will be null if none of the radio buttons are selected radioGroup1.clearCheck(); PdBase.sendFloat("D1", 0); } }); 
+4
source share
3 answers

Please see my answer to your other basically identical question , which I copied below ... :)

Recently, I had the same need - to have a radio group in which the selected item could be canceled by clicking on it again. I found that I was not able to accomplish this using listeners, but I was able to do this using a custom RadioButton , for example ...

 public class ToggleableRadioButton extends RadioButton { // Implement necessary constructors @Override public void toggle() { if(isChecked()) { if(getParent() instanceof RadioGroup) { ((RadioGroup)getParent()).clearCheck(); } } else { setChecked(true); } } } 

Notice that the button switches differently depending on its current state, i.e. it calls setChecked(true) on the button and calls clearCheck() in the group. If setChecked() used in both cases, the button that was simply canceled cannot be immediately re-selected - the logic in RadioGroup prevents it.

To use this button, simply replace the <RadioButton> tags with <your.package.ToggleableRadioButton> in your XML layout.

+20
source

You need to put the button in a group of radio stations, and then clear the group. You cannot immediately turn off the switch because the idea is that one parameter in a group is always checked.

+1
source

Here is an example of how to create a radio group, a radio object, and how to use it in Java code. Hope this will give you the whole picture.

XML file:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RadioGroup android:id="@+id/maptype" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/line1" android:orientation="horizontal" > <RadioButton android:id="@+id/map" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:drawableRight="@drawable/ic_launcher"/> <RadioButton android:id="@+id/satellite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableRight="@drawable/ic_launcher"/> </RadioGroup> </LinearLayout> 

In java code

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); RadioGroup rgrpMapType = (RadioGroup) findViewById(R.id.maptype); int selectedTypeId = rgrpMapType.getCheckedRadioButtonId(); RadioButton rbMapType = (RadioButton) findViewById(selectedTypeId); if(rbMapType != null) // This will be null if none of the radio buttons are selected rgrpMapType.clearCheck(); } 
+1
source

All Articles