android - 只有一个选定项目的 RadioGroup

标签 android radio-button radio-group

我正在尝试创建一个 RadioGroup,如图所示。我希望一次只检查一个 RadioButton

这就是我为实现这一目标所做的。

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/radio1" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/radio2" />
    </RadioGroup>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/radio3" />

        <RadioButton
            android:id="@+id/radio4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/radio4" />
    </RadioGroup>
</RadioGroup>

这是图片

What I want

但是通过使用它我可以从每一行中选择任何一个,即可以选择 2 个项目。 像这样:

enter image description here

请帮帮我...

最佳答案

enter image description here

已更新

您可以在单击其他 RadioButton 时动态取消选择 RadioButton。为此,我不得不尝试将 View 添加到 ViewGroup,这是通过获取 TableRow< 的所需 index 来确定的.

如上图所示,它应该以网格状布局排列,并且在任何给定时间只能选择一个单选按钮。引用以下代码:

.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.example.test.RadioButtonWithTableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TableRow>

        <RadioButton
            android:id="@+id/radio1"
            android:text="Radio 1" />

        <RadioButton
            android:id="@+id/radio3"
            android:text="Radio 3" />
    </TableRow>

    <TableRow>

        <RadioButton
            android:id="@+id/radio2"
            android:text="Radio 2" />

        <RadioButton
            android:id="@+id/radio4"
            android:text="Radio 4" >
        </RadioButton>
    </TableRow>

</com.example.test.RadioButtonWithTableLayout>

.java:

public class RadioButtonWithTableLayout extends TableLayout implements
        OnClickListener {

    private RadioButton mBtnCurrentRadio;

    public RadioButtonWithTableLayout(Context context) {
        super(context);
    }

    public RadioButtonWithTableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onClick(View v) {
        final RadioButton mBtnRadio = (RadioButton) v;

        // select only one radio button at any given time
        if (mBtnCurrentRadio != null) {
            mBtnCurrentRadio.setChecked(false);
        }
        mBtnRadio.setChecked(true);
        mBtnCurrentRadio = mBtnRadio;
    }

    @Override
    public void addView(View child, int index,
            android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        setChildrenOnClickListener((TableRow) child);
    }

    @Override
    public void addView(View child, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, params);
        setChildrenOnClickListener((TableRow) child);
    }

    private void setChildrenOnClickListener(TableRow tr) {
        final int c = tr.getChildCount();
        for (int i = 0; i < c; i++) {
            final View v = tr.getChildAt(i);
            if (v instanceof RadioButton) {
                v.setOnClickListener(this);
            }
        }
    }
}

来源: http://developer.android.com/reference/android/widget/RadioGroup.html http://developer.android.com/reference/android/widget/TableRow.html

关于android - 只有一个选定项目的 RadioGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16811451/

相关文章:

javascript - 单选按钮 'onclick' 在 Chrome 和 Firefox 中有效,但在 IE 中无效

jQuery radio onchange 切换父元素的类?

javascript - 带过滤器的高级搜索 - Angularjs

android - 检查 Recyclerview Adapter 中的单选按钮状态并从 MainActivity 对其应用更改

android - RadioButton Get(java.lang.IllegalStateException:selectedRadioButton不能为null)

php - 如何使用 PHP 原生函数和 Android 获取文件

android - 从共享首选项中恢复字符串

android - 顶部的上下文菜单

android - 使用 play-services-xxx :9. 8.00 后 Firebase 错误无法访问 zzanb

javascript - 使用 getElementsByName 验证单选按钮