5

我在一个单选组中有两个单选按钮。我还有 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"/>
4

5 回答 5

14

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

于 2012-04-11T14:13:52.410 回答
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.

于 2016-12-13T13:48:57.207 回答
0

To check/uncheck radio buttons not part of a RadioGroup, add a method in the android GUI designer under the RadioButton Properties 'onClick' (In this example radio1_onClick).

In your activity.xml under the xml for the RadioButton

RadioButton 
    [blah blah blah]
    android:id="@+id/radio1"
    android:onClick="radio1_onClick"

In your activity_main.xml you would write the following method.

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 */
}
于 2013-06-29T13:54:01.057 回答
0

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

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

于 2012-04-11T14:33:29.683 回答
0

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

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

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

如何使用自定义可绘制对象自定义 RadioButtons?

在这里查看答案。

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

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

希望这会帮助你。

于 2013-05-17T20:53:53.863 回答