繁体   English   中英

如何取消选中另一个广播组中的一个单选按钮?

[英]How to uncheck a radio button in another radio group?

我在一个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="wrap_content"
    android:orientation="horizontal">

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

       <RadioButton
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked" />

        <RadioButton
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked" />

    </RadioGroup>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked" />

        <RadioButton
            android:id="@+id/btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked" />

    </RadioGroup>

</LinearLayout>

在主要活动中,我可以通过实现onRadioButtonClicked()方法来处理Click侦听器上的单选按钮:

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.btn_1:

            break;
        case R.id.R.id.btn_2: 

            break;
        case R.id.R.id.btn_3: 

            break;
        case R.id.R.id.btn_4: 

            break;
    }
}

现在,在同一单选按钮组中,如果选中了一个单选按钮,则另一个将自动取消选中。 所以这是我的问题:我希望如果一个按钮被选中,则同一组和另一个组中的其他按钮将被取消选中。 这可行吗? 谢谢。

您可以手动进行。

case R.id.btn_1:
   btn3.setChecked(false);
   btn4.setChecked(false);
  break;
case R.id.btn_2:
   btn3.setChecked(false);
   btn4.setChecked(false);
  break;
case R.id.btn_3:
   btn1.setChecked(false);
   btn2.setChecked(false);
  break;
case R.id.btn_4:
   btn1.setChecked(false);
   btn2.setChecked(false);
  break;

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM