5

只是想看看是否有人知道是否有单选组或单选按钮属性或其他快速允许单选按钮在处于选中模式时被取消选中的东西。我正在寻找构建像无线电组一样工作的功能(即只能检查一个),但我也希望它们都能够不被选中。

4

2 回答 2

11

也许我在这里没有得到问题,但这是我想做的事情。我有一个活动用于对许多图片进行分类。我正在使用单选按钮进行分类。在用户检查其中一个选项后,他可以切换到下一张图片。切换图片时,我需要清除选择,但决定不创建新活动。

所以我在布局中初始化我的广播组,如下所示:

<RadioGroup
    android:id="@+id/radio_selection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radio_true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/true" />

    <RadioButton
        android:id="@+id/radio_false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/false" />
</RadioGroup>

这会初始化我的无线电组,并且RadioButton最初都没有选择它们。之后,当我更改图片时,我需要清除选择(因为用​​户尚未选择新图片)。我喜欢这样:

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_selection);
radioGroup.clearCheck();

这正是我所需要的:再次没有选择任何单选按钮。我希望我理解这个问题,这将有助于将来的人。

于 2012-04-07T14:13:15.193 回答
3

您可以使用 CheckBox 来模仿您想要的功能,如下所示。该代码假定您有两个复选框,但您可以有两个以上。

public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.checkBox1) {
        // Toggle status of checkbox selection
        checkBox1Selected = checkBox1.isChecked();

        // Ensure that other checkboxes are not selected
        if (checkBox2Selected) {
            checkBox2.setChecked(false);
            checkBox2Selected = false;
         } 
    else if (id == R.id.checkBox2) {
         // Toggle status of checkbox selection
         checkBox2Selected = checkBox2.isChecked();

        // Ensure that other checkboxes are not selected
        if (checkBox1Selected) {
            checkBox1.setChecked(false);
            checkBox1Selected = false;
        }
}
于 2011-06-22T03:01:07.083 回答