一次只能选择一个RadioButton

时间:2017-09-25 17:03:17

标签: android

当我点击TextView引用时,我正在获取AlertDialog构建器,因为我显示employeesNamesRadioButtons的列表。现在当我选错了Employee并且我想要选择其他employee时,应该取消选中错误的employee名称,并选择一个Employee,{{1}应启用ok

Button

请帮我编写代码,不要发表我无法理解的理论我是初学者。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用RadioGroup而不是RadioButton。

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/a"
    android:onClick="onRGClick">

    <RadioButton
        android:text="Normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/a1" />

    <RadioButton
        android:text="Tidak normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/a2" />

</RadioGroup>

并尝试 setOnCheckedChangeListener 方法进行选择并检索所选项目

radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.a1) {
            // do your stuff
        } else if (checkedId == R.id.a2) {
            // do your stuff
        }
    }
});
相关问题