RadioGroup with only one selected Item(只有一个选定项目的 RadioGroup)
问题描述
我正在尝试制作如图所示的 RadioGroup.我希望一次只检查一个 RadioButton.
I am trying to make a RadioGroup as in the image. I want that only one RadioButton should be checked at a time.
这就是我为实现这一目标所做的.
This is what I did to achive this.
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/radio1" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/radio2" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/radio3" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/radio4" />
</RadioGroup>
</RadioGroup>
这是图片
但是通过使用它,我可以从每一行中选择任何一个,即可以选择 2 个项目.像这样:
But by using this I can select any one from each row, i.e. 2 items can be selected. Like this:
请帮帮我...
推荐答案
更新
您可以在单击其他 RadioButton 时动态取消选择 RadioButton.为此,我不得不尝试将 View 添加到 ViewGroup,这是通过获取 TableRow<的所需 index 来确定的/代码>.
You can dynamically unselect the RadioButton when other RadioButton is clicked. To do so, I had to play with adding View to ViewGroup, which is determined by getting its desired index of the TableRow.
如上图所示,它应该以网格状布局排列,在任何给定时间只选择一个单选按钮.参考以下代码:
As shown from the above picture, it should be arranged in grid-like layout with only one radio button selected at any given time. Refer to the codes below:
.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.example.test.RadioButtonWithTableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow>
<RadioButton
android:id="@+id/radio1"
android:text="Radio 1" />
<RadioButton
android:id="@+id/radio3"
android:text="Radio 3" />
</TableRow>
<TableRow>
<RadioButton
android:id="@+id/radio2"
android:text="Radio 2" />
<RadioButton
android:id="@+id/radio4"
android:text="Radio 4" >
</RadioButton>
</TableRow>
</com.example.test.RadioButtonWithTableLayout>
.java:
public class RadioButtonWithTableLayout extends TableLayout implements
OnClickListener {
private RadioButton mBtnCurrentRadio;
public RadioButtonWithTableLayout(Context context) {
super(context);
}
public RadioButtonWithTableLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onClick(View v) {
final RadioButton mBtnRadio = (RadioButton) v;
// select only one radio button at any given time
if (mBtnCurrentRadio != null) {
mBtnCurrentRadio.setChecked(false);
}
mBtnRadio.setChecked(true);
mBtnCurrentRadio = mBtnRadio;
}
@Override
public void addView(View child, int index,
android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
setChildrenOnClickListener((TableRow) child);
}
@Override
public void addView(View child, android.view.ViewGroup.LayoutParams params) {
super.addView(child, params);
setChildrenOnClickListener((TableRow) child);
}
private void setChildrenOnClickListener(TableRow tr) {
final int c = tr.getChildCount();
for (int i = 0; i < c; i++) {
final View v = tr.getChildAt(i);
if (v instanceof RadioButton) {
v.setOnClickListener(this);
}
}
}
}
来源:http://developer.android.com/reference/android/widget/RadioGroup.htmlhttp://developer.android.com/reference/android/widget/TableRow.html
这篇关于只有一个选定项目的 RadioGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:只有一个选定项目的 RadioGroup
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
