RadioGroup with two columns which have ten RadioButtons(RadioGroup 有两列有十个 RadioButtons)
问题描述
我有一个 RadioGroup
,我想在两列五行中将按钮彼此相邻对齐,但我无法实现.我尝试过的事情:
I have a RadioGroup
and I want to align buttons next to each other in two columns and five rows and I am unable to achieve it. Things I have tried:
RelativeLayout
->RadioGroup
外部 ->RadioGroup
内部.所有RadioButtons
都被选中,但我只想选中一个.RadioGroup
:方向- 跨度、拉伸列
表格行
表格布局
RelativeLayout
-> OutsideRadioGroup
-> InsideRadioGroup
. AllRadioButtons
are selected, but I want only one to be selected.RadioGroup
: orientation- Span, stretchcolumns
TableRow
TableLayout
请告诉我如何创建一个 RadioGroup
并在其中包含两列和许多 RadioButtons
.
Please let me know how create one RadioGroup
and have two columns and many RadioButtons
within.
推荐答案
你可以模拟那个 RadioGroup
让它看起来像你只有一个.例如,您有 rg1
和 rg2
(RadioGroups
方向设置为 vertical
(两列)).要设置这些 RadioGroups
:
You can simulate that RadioGroup
to make it look like you have only one. For example you have rg1
and rg2
(RadioGroups
with orientation set to vertical
(the two columns)). To setup those RadioGroups
:
rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
rg1.clearCheck(); // this is so we can start fresh, with no selection on both RadioGroups
rg2.clearCheck();
rg1.setOnCheckedChangeListener(listener1);
rg2.setOnCheckedChangeListener(listener2);
要在这些 RadioGroups
中仅选择一个 RadioButton
,上面的侦听器将是:
To select only one RadioButton
in those RadioGroups
the listeners above will be:
private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
rg2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
rg2.clearCheck(); // clear the second RadioGroup!
rg2.setOnCheckedChangeListener(listener2); //reset the listener
Log.e("XXX2", "do the work");
}
}
};
private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
rg1.setOnCheckedChangeListener(null);
rg1.clearCheck();
rg1.setOnCheckedChangeListener(listener1);
Log.e("XXX2", "do the work");
}
}
};
要从 RadioGroups
中获取选中的 RadioButton
,您可以这样做:
To get the checked RadioButton
from the RadioGroups
you could do:
int chkId1 = rg1.getCheckedRadioButtonId();
int chkId2 = rg2.getCheckedRadioButtonId();
int realCheck = chkId1 == -1 ? chkId2 : chkId1;
如果你使用 RadioGroup
的 check()
方法,你必须记住在另一个 上调用
.clearCheck()
无线电组
If you use the check()
method of the RadioGroup
you have to remember to call clearCheck()
on the other Radiogroup
.
这篇关于RadioGroup 有两列有十个 RadioButtons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:RadioGroup 有两列有十个 RadioButtons
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01