开发者

Radio Button does not toggle its state

I add a RadioButton in my layout.

It is unchecked to begin with. And when I click on it , it becomes checked (as shown in emulator). But when whe开发者_JAVA技巧n i click on it again, it does not become unchecked again?

<RadioButton android:checked="false"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:id="@+id/option1"/>


If you're looking for checkbox behavior with radio button appearance, you could pass in the xml style to a checkbox.

<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/>

This can be useful in some cases (ex. using radio buttons in a RecyclerView) but you should be careful because the user expects radio buttons to behave a certain way. If you're allowing the user to make multiple selections you should probably use a normal checkbox, as mentioned in the comments above.

Hope this helps!


If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.

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

Scroll down and see checkbox and toggle button.

When using radios you usually have more than one and choose between them. Like easy, medium, hard.


If you are having more than 1 radio buttons to work with then add "RadioGroups" as follows:

<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>

Have a look at this example , i am sure this will make your idea clear regarding Radio Buttons.

Also refer this Android Developer - Form Stuff page .


After searching a lot on SO, I came up with a not-so-good but decent workaround.

Declare a boolean variable for each RadioButton you have, initialize it with false, and change the state of the variable and the RadioButton at every click.

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

  1. I know it's ideal to use a Checkbox, but if someone needs the radiobutton, then they need the radiobutton.

  2. This is not an optimal solution, as it will basically toggle the button twice every time the user clicks the button (one is the default behaviour, the second time is inside your onclick function), so if you're using an OnCheckedChangeListener you will probably get two calls for the same click.

  3. There's another workaround, which is to change the android:button in a checkbox to another drawable with an xml template, but it's a bit more complex, requires at least 2 more files for the states.


That is a conceptual question: Radio buttons allow you to choose between several options (represented by the other radio buttons). Typically, one radio button out of a group is always checked, even if in an initial state no buttons may be checked if you do not declare one as a default value. That means also that a single button does not allow to toggle its state unless other radio buttons are present in the same group - if there is only one option, you will have to choose it.

If you want a binary toggle, you will want to use a checkbox instead.


Solution is set checked[true/false] intermediate in Java code

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜