一次只选择一个单选按钮 [英] Only one radio button selected at a time

查看:21
本文介绍了一次只选择一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个单选组中有两个单选按钮.我还有 2 个 androd:button checkbox- 用于取消选择单选按钮时,以及 checkbox_v 用于用户选择复选框时.我还实现了一个方法 onRadioButtonClick 以确保只有一个单选按钮具有 drawable:checkbox 而另一个具有 checkbox_v .我怎样才能实现 onRadioClick 来做到这一点?有什么想法吗?

I have two radio button in a radio group. I also have 2 androd:button checkbox- for when the radio button is deselected and checkbox_v for when he user selects the checkbox. I also implemnted that a method onRadioButtonClick in order to make sure that only one radio button had drawable:checkbox and the other has checkbox_v . How can I implemnt onRadioClick to do this? any idea?

public void onRadioButtonClick(View v)
{
    RadioButton button = (RadioButton) v;
    boolean checkBox1Selected;
    boolean checkBox2Selected = false;

    Toast.makeText(MainActivity.this,
        button.getId()+ " was chosen."+R.id.wificheckBox,
        Toast.LENGTH_SHORT).show();

    if ( button.getId() ==R.id.wificheckBox) {
        // Toggle status of checkbox selection
        checkBox1Selected = radiowifiButton.isChecked();

        // Ensure that other checkboxes are not selected
        if (checkBox2Selected) {
            radiomobileButton.setChecked(false);
            checkBox2Selected = false;
        }
        else if (button.getId() ==R.id.wifimobilecheckBox) {
            // Toggle status of checkbox selection
            checkBox2Selected = radiomobileButton.isChecked();
            // Ensure that other checkboxes are not selected
            if (checkBox1Selected) {
                radiowifiButton.setChecked(false);
                checkBox1Selected = false;
            }
        }
    }

主xml

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true"
    android:layout_below="@+id/ll_1"
    android:layout_marginLeft="20dp">

<LinearLayout
    android:id="@+id/ll_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ll_1"
    android:layout_marginLeft="20dp"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/wifimobilecheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/button_radio"
        android:checked="true"
        android:onClick="onRadioButtonClick" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/wificheckBox"
        android:layout_toRightOf="@+id/wificheckBox"
        android:paddingLeft="15dp"
        android:text="WiFi or mobile network"
        android:textColor="#333333"
        android:textSize="20dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/ll_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ll_2"
    android:paddingTop="20dp"
    android:layout_marginLeft="20dp"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/wificheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/button_radio"

        android:onClick="onRadioButtonClick" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/wificheckBox"
        android:layout_toRightOf="@+id/wificheckBox"
        android:paddingLeft="15dp"
        android:text="WiFi "
        android:textColor="#333333"
        android:textSize="20dp" />
</LinearLayout>

</RadioGroup>

Drawabe- button_radio

Drawabe- button_radio

<item android:state_checked="true"android:state_pressed="false" android:drawable="@drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="@drawable/checkbox"/>

推荐答案

如果他们在一个radiogroup中,一次只能选择一个.如果您希望两者都能够被选中,请将它们从无线电组中删除.

If they are in a radiogroup, only one can be selected at a time. If you want both to be able to be selected, remove them from the radiogroup.

这篇关于一次只选择一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆