在两个线性布局中使用时无法取消选中单选按钮



>我使用两种线性布局在一行中显示两行单选按钮 3,在另一行中显示 3。我已经搜索并发现我们应该使用radio group.但是使用单选按钮组后,当我选择单选按钮时,我也无法取消选中单选按钮

<RadioGroup
    android:id="@+id/payment_mode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="18dp"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/cash"
            android:text="CASH" />
        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/card"
            android:text="CARD" />
        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/pay_tm"
            android:text="PAYTM" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/bank"
            android:text="BANK" />
        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/cc_av"
            android:text="CC-AV" />
        <RadioButton
            style="@style/LinkButton"
            android:id="@+id/credit"
            android:text="CREDIT" />
    </LinearLayout>
</RadioGroup>

来自开发者网站

要创建每个单选按钮选项,请在 布局。但是,由于单选按钮是互斥的,因此 必须在单选机组中将它们组合在一起。通过对它们进行分组 总之,系统确保只能有一个单选按钮 一次选定。

因此,单选按钮的直接父级必须是 RadioGroup,在布局文件中,直接父级是 LinearLayout。删除线性布局并将单选按钮直接放在单选组下方。

您不能在 RadioGroup 中使用任何布局。您只能使用 RadioButton,否则RadioButton将不起作用。 RadioButton 应该是无线电组的直系子级。

通过将单选组的方向更改为水平,可以解决您的问题。更多信息在这里

对于我的答案,您不需要使用单选组。您只需在每个单选按钮上设置单击侦听器即可。例如。

RadioButton tempRadioButton;   //global variable.
radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (tempRadioButton != null){
                        tempRadioButton.setChecked(false);
                    }
                    tempRadioButton = (RadioButton) view;
                }
            });

还可以使用此方法在列表视图或回收器视图的每个项目上设置单选按钮。

尝试像这样更改布局并执行单击操作,请参阅此链接:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioGroup
            android:id="@+id/payment_mode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:orientation="vertical">
            <RadioButton
                android:id="@+id/cash"
                style="@style/LinkButton"
                android:text="CASH" />
            <RadioButton
                android:id="@+id/card"
                style="@style/LinkButton"
                android:text="CARD" />
            <RadioButton
                android:id="@+id/pay_tm"
                style="@style/LinkButton"
                android:text="PAYTM" />
            <RadioButton
                android:id="@+id/bank"
                style="@style/LinkButton"
                android:text="BANK" />

            <RadioButton
                android:id="@+id/cc_av"
                style="@style/LinkButton"
                android:text="CC-AV" />

            <RadioButton
                android:id="@+id/credit"
                style="@style/LinkButton"
                android:text="CREDIT" />
        </RadioGroup>
    </LinearLayout>

最新更新