【发布时间】: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 以防止用户选择单选选项吗?