取消选择单选按钮

4

这个应用是一个16组单选按钮步进应用程序,每组有8个按钮。除了一组按钮被选中后,我不能将其关闭,除非使用我创建的清除按钮清除所有的单选按钮组。我想添加的代码是当再次选择已选中的单选按钮时,它就像切换开关一样被关闭。我尝试使用切换开关,但是那种方法会导致其他问题。下面是我的一次尝试,但是我猜测它偏离了正确方向。

final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup1);
RadioButton lC1 = (RadioButton)findViewById(R.id.RadioButtonlowC1);

Button D1 = (Button)findViewById(R.id.RadioButtonD1);
        D1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                PdBase.sendFloat("D1", 74);
                int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
                RadioButton D1 = (RadioButton) findViewById(selectedTypeId);
                if(radioGroup1 != null) // This will be null if none of the radio buttons are selected
                       radioGroup1.clearCheck(); 
                PdBase.sendFloat("D1", 0);
            }
        });
2个回答

1
这是一个创建单选组、单选按钮并在Java代码中使用的示例。希望它能给你完整的图片。
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <RadioGroup
        android:id="@+id/maptype"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/line1"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/map"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:drawableRight="@drawable/ic_launcher"/>

        <RadioButton
            android:id="@+id/satellite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/ic_launcher"/>
    </RadioGroup>

</LinearLayout>

在Java代码中。
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    RadioGroup rgrpMapType = (RadioGroup) findViewById(R.id.maptype);
    int selectedTypeId = rgrpMapType.getCheckedRadioButtonId();
    RadioButton rbMapType = (RadioButton) findViewById(selectedTypeId);
    if(rbMapType != null) // This will be null if none of the radio buttons are selected
           rgrpMapType.clearCheck(); 
}

1
在上面的Java代码的第3行之后编写以下代码。只有在选择了某个单选按钮时,rbMapType才为null。希望代码很清楚。如果(rbMapType != null),则rgrpMapType.clearCheck(); - Joseph Selvaraj
谢谢回复,但是当我实现你给我的代码后,它不让我选择单选按钮,这是添加你的代码后的效果。 - A B
1
无法修复,所以我打算为每个组添加一个清除按钮。 - A B

1

你需要将按钮放在单选按钮组中,然后清除该组。无法直接取消单选按钮的选择,因为其设计理念是在一组中始终只有一个选项被选中。


它在一个单选按钮组中,很抱歉我有一个按钮可以清除所有按钮,但那是针对所有单选按钮组的。实际上,我也会尝试更具体地针对每个组使用那段代码。 - A B
实际上这对我不起作用,因为我需要能够在单选按钮组内切换到不同的按钮。 - A B
给定完整的代码...现在当应用程序加载时,它将重置并确保没有单选按钮被选中。一旦应用程序在我的系统中加载,我就能够选择文件。 - Joseph Selvaraj
1
我不同意“单选按钮组的想法是组中的一个选项始终被选中。”这个想法是为了强制互斥。一次只能选择一个选项,但没有选择也是完全有效的。默认情况下,组没有选择,正如你所提到的,可以通过编程方式清除组的选择(例如按下按钮的结果)。问题仅仅是要求以不同的操作方式清除组 - 点击选定的<RadioButton>按钮而不是添加“清除”<Button>。请参见我的解决方案。 - spaaarky21

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接