【问题标题】:RadioGroup - RadioButton is not selected on first clickRadioGroup - 首次单击时未选择 RadioButton
【发布时间】:2012-08-13 00:41:57
【问题描述】:

我在一个对话框视图中有两个单选按钮组,供我的用户选择他们想要的显示颜色。由于大小限制,我创建了两个组。因为这两个组在分开时不会保持互斥,我需要检查从 group1 到 group2 的更改并清除来自另一个组的选择。我通过向每个组添加一个 onCheckedChangeListener() 来做到这一点,如下所示:

    RadioGroup rGroup1 = (RadioGroup)currentDialog.findViewById(R.id.rgColors1);
     rGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId != -1)
            {
                RadioGroup rGroup2 = (RadioGroup) currentDialog.findViewById(R.id.rgColors2);
                rGroup2.clearCheck();
            }
        }

      });

     RadioGroup rGroup2 = (RadioGroup) currentDialog.findViewById(R.id.rgColors2);
     rGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId != -1)
            {
                RadioGroup rGroup1 = (RadioGroup) currentDialog.findViewById(R.id.rgColors1);
                rGroup1.clearCheck();
            }
        }

     });

我的对话框布局如下所示:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/colorDialogLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:minWidth="300dp"
android:orientation="vertical">
<TableLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tableLayout"
    android:padding="5dp">
    <TableRow android:gravity="center">
        <RadioGroup android:id="@+id/rgColors1">
            <RadioButton android:id="@+id/black_radio" 
                android:text="@string/option_black" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton android:id="@+id/blue_radio" 
                android:text="@string/option_blue" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton android:id="@+id/green_radio" 
                android:text="@string/option_green" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton android:id="@+id/orange_radio" 
                android:text="@string/option_orange" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton android:id="@+id/pink_radio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/option_pink" />
        </RadioGroup>
        <RadioGroup android:id="@+id/rgColors2">
            <RadioButton android:id="@+id/purple_radio" 
                android:text="@string/option_purple" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton android:id="@+id/red_radio" 
                android:text="@string/option_red" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton android:id="@+id/teal_radio" 
                android:text="@string/option_teal" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton android:id="@+id/yellow_radio" 
                android:text="@string/option_yellow" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </RadioGroup>
    </TableRow>
</TableLayout>
<Button android:id="@+id/setBackColorButton"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"  
  android:layout_gravity="center_horizontal"
  android:text="@string/button_set_back_color"/>

当从任一组中选择单选按钮时,它不属于的组将被清除。我遇到的问题是选中的单选按钮未显示为选中状态,必须再次选择。我怀疑 clearCheck() 正在为两个无线电组运行,但我不确定为什么或如何防止这种行为。任何帮助将不胜感激。

【问题讨论】:

  • 请说明单选组/单选按钮之间的关系。
  • 我添加了我的布局 xml。如您所见,我有一个带有两个显示在我身边的无线电组的表格视图。显然我只想选择一种颜色,但有两组 android 不会自动执行此操作。这就是为什么我添加了 clearCheck() 但它不能正常工作。
  • 你试过设置focusable=false吗?
  • 不会将 focusable 设置为 false 以防止用户选择单选选项吗?

标签: android android-widget


【解决方案1】:

最后,我必须为每个单选按钮创建一个 onclick() 事件。我认为我的第一种方法不起作用的原因是因为在将选择设置为无时触发了 onselectchange() 事件。在测试使用这种将第二组设置为无的方法的各种方法中,导致需要第二次执行选择或两组相互设置为无的无限循环。我相信在需要第二次执行选择的情况下是因为程序没有将控制权返回给调用程序来完成选择。我对 Android 有点陌生,所以我可以完全脱离基地。最后,单独的 onclick() 事件确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2016-09-15
    相关资源
    最近更新 更多