一次只能选择一个单选按钮

时间:2012-04-11 14:03:55

标签: android radio-button radio-group

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

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

main 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

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

5 个答案:

答案 0 :(得分:14)

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

答案 1 :(得分:4)

作为最高投票的答案对我来说并不像许多其他人那样有效。我正在分享我使用的方法和它的完美工作。

<RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkedButton="@+id/rb_female"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="10dp"
                android:layout_marginRight="10dp"
                android:button="@drawable/selector_radio_button"
                android:text="Female" />

            <RadioButton
                android:id="@+id/rb_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/selector_radio_button"
                android:text="Male" />
</RadioGroup>

android:checkedButton="@+id/rb_female"设置RadioGroup为我工作。

答案 2 :(得分:0)

您应该删除onRadioButtonClick方法并将OnCheckedChangeListener添加到您的radiogroup。 http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

我想通过实现你自己的点击处理程序,你将覆盖radiogroup的一些功能。

答案 3 :(得分:0)

您的要求不明确。你介意把它变得更简单吗?我尽力了解您的要求,这些是以下结果:(。

单选按钮可以包含文字。因此,您不必在布局中添加其他TextView。请删除它们,并在您的单选按钮上添加一个简单的android:text="blah.. blah.."

根据我的理解,你有两个疑问。

如何使用自定义drawables自定义RadioButtons?

查看答案here

如何取消选择先前选择的单选按钮?

事实上,您不必手动执行此操作,因为您将这些单选按钮保持在一个单一的无线电组中。

希望这会对你有所帮助。

答案 4 :(得分:0)

要检查/取消选中不属于RadioGroup的单选按钮, 在android GUI设计器下添加一个方法 RadioButton Properties'onClick'(在本例中为radio1_onClick)。

在RadioButton

的xml下的activity.xml中
RadioButton 
    [blah blah blah]
    android:id="@+id/radio1"
    android:onClick="radio1_onClick"

在您的activity_main.xml中,您将编写以下方法。

private void radio1_onClick(View v)
{
    CompoundButton b=(CompoundButton)findViewById(R.id.radio1);
    b.setChecked(true); /* light the button */

    //b.setChecked(false); /* unlight the button */
}
相关问题