【问题标题】:Only one radio button selected at a time一次只选择一个单选按钮
【发布时间】:2012-04-23 20:27:01
【问题描述】:

我在一个单选组中有两个单选按钮。我还有 2 个 androd:button checkbox- 用于取消选择单选按钮时,以及 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;
            }
        }
    }

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

【问题讨论】:

  • 你试过什么?在此处粘贴您的代码

标签: android radio-button radio-group


【解决方案1】:

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

【讨论】:

  • 不正确。我正在使用自己的可绘制绘图/复选框和可绘制/复选框_v而不是标准单选按钮自定义单选按钮。目前我可以选择两个radi按钮并且都设置图像drawable/checkbox_v
【解决方案2】:

因为投票最多的答案对我和其他许多人一样不起作用。我正在分享我使用的方法及其完美运行。

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

RadioGroup 中设置android:checkedButton="@+id/rb_female" 对我有用。

【讨论】:

    【解决方案3】:

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

    我想通过实现您自己的点击处理程序,您正在覆盖无线电组的某些功能。

    【讨论】:

      【解决方案4】:

      您的要求不清楚。你介意让它更简单吗?我尽力了解您的要求,这些是下面给出的调查结果:(。

      单选按钮可以有文本。因此,您不必在布局中添加另一个 TextView。请删除它们并将简单的android:text="blah.. blah.." 添加到您的单选按钮。

      据我了解,您有两个疑问。

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

      查看答案here

      如何取消选中之前选中的单选按钮?

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

      希望这会对你有所帮助。

      【讨论】:

        【解决方案5】:

        要选中/取消选中不属于 RadioGroup 的单选按钮, 在 android GUI 设计器中添加一个方法 RadioButton 属性“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 */
        }
        

        【讨论】:

          猜你喜欢
          • 2017-06-28
          • 1970-01-01
          • 1970-01-01
          • 2016-01-09
          • 2011-08-13
          • 2018-08-01
          • 1970-01-01
          • 2023-04-10
          • 1970-01-01
          相关资源
          最近更新 更多