Radio button does not switch its state

I am adding RadioButton to my layout.

It is not installed to start. And when I click on it, it becomes checked (as shown in the emulator). But when, when I click on it again, it will not be removed again?

<RadioButton android:checked="false"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:id="@+id/option1"/>
+5
source share
6 answers

If you use only one switch to test and turn off, perhaps you should use a checkbox or switch button.

http://developer.android.com/resources/tutorials/views/hello-formstuff.html

Scroll down and see the checkbox and the toggle button.

When using radio stations, you usually have more than one and choose between them. Like light, medium, hard.

+3
source

, xml- .

<CheckBox android:checked="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/option1"
         style="@android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton/>

(, RecyclerView), , , . , , , , .

, !

+5

1 , "RadioGroups" :

<RadioGroup android:id="@+id/group1" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="vertical">
        <RadioButton android:id="@+id/radio1" android:text="madras"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
        <RadioButton android:id="@+id/radio2" android:text="bombay"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </RadioGroup>

, , .

Android - .

+3

: ( ). , , , . , , - , .

, .

+1

[true/false] Java-

ToggleButton t = (ToggleButton) findViewById(R.id.toggle_button);
t.setChecked(true); 
// t.setChecked(false);
+1

SO, , .

boolean RadioButton, false RadioButton .

boolean isToggledRadio1 = false;
RadioButton radio1 = (RadioButton) findViewById(R.id.radiobutton1);
radio1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        isToggledRadio1 = !isToggledRadio1; //Switch boolean value
        RadioButton rb = (RadioButton)v;
        rb.setChecked( isToggledRadio1 );
    }
});

  • , , - , .

  • , , ( , onclick), , OnCheckedChangeListener, , , .

  • There is another workaround, which is to change android:buttonthe checkbox to another, with the ability to draw using the xml template, but it is a little more complicated, it requires at least 2 more files for states.

+1
source

All Articles