RadioGroup中有两列其中有十个单选按钮 [英] RadioGroup with two columns which have ten RadioButtons

查看:157
本文介绍了RadioGroup中有两列其中有十个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RadioGroup中,我想在两列,5行排列的按钮旁边的对方,我无法去实现它。我已经试过的东西:

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:

  1. RelativeLayout的 - >外 RadioGroup中 - >在 RadioGroup中。 所有单选按钮选择,但我想只有一个被选中。
  2. RadioGroup中:方向
  3. 跨度,stretchcolumns
  4. 的TableRow
  5. TableLayout
  1. RelativeLayout -> Outside RadioGroup -> Inside RadioGroup. All RadioButtons are selected, but I want only one to be selected.
  2. RadioGroup : orientation
  3. Span, stretchcolumns
  4. TableRow
  5. TableLayout

请让我知道如何创建一个 RadioGroup中并有两列,许多单选按钮之内。

Please let me know how create one RadioGroup and have two columns and many RadioButtons within.

推荐答案

您可以模拟该 RadioGroup中来使它看起来像你只有一个。比如你有皂苷Rg1 RG2 (RadioGroups <$ C C $>与方向设置为垂直(两列))。要设置这些 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 的听众将是:

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 ,你可以这样做:

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中你要记得叫<$ C $的检查()方法C> clearCheck()另 RadioGroup中

If you use the check() method of the RadioGroup you have to remember to call clearCheck() on the other Radiogroup.

这篇关于RadioGroup中有两列其中有十个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆