如何从不同LinearLayouts组单选按钮? [英] How to group RadioButton from different LinearLayouts?

查看:803
本文介绍了如何从不同LinearLayouts组单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有可能组的每个单独的单选在一个独特的 RadioGroup中 保持相同的结构。我的结构是这样的:

I was wondering if is possible to group each single RadioButton in a unique RadioGroup maintaining the same structure. My structure look like this:

  • LinearLayout_main
    • LinearLayout_1
      • RadioButton1
      • LinearLayout_main
        • LinearLayout_1
          • RadioButton1
          • RadioButton2
          • RadioButton3

          正如你可以看到,现在每个单选是不同的子的LinearLayout 。我试着用下面的结构,但它不工作:

          As you can see, now each RadioButton is a child of different LinearLayout. I tried using the structure below, but it doesn't work:

          • 在RadioGroup中
            • LinearLayout_main
              • LinearLayout_1
                • RadioButton1
                • Radiogroup
                  • LinearLayout_main
                    • LinearLayout_1
                      • RadioButton1
                      • RadioButton2
                      • RadioButton3

                      推荐答案

                      看来,好人在谷歌/ Android的假设,当你使用单选按钮,你不需要自带了Android的所有其他方面的灵活性UI /布局系统。简单地说:他们不希望你嵌套布局和单选按钮。叹了口气。

                      It seems that the good people at Google/Android assume that when you use RadioButtons, you don't need the flexibility that comes with every other aspect of the Android UI/layout system. To put it simply: they don't want you to nest layouts and radio buttons. Sigh.

                      所以,你必须解决该问题。也就是说,你必须实现自己单选按钮。

                      So you gotta work around the problem. That means you must implement radio buttons on your own.

                      这真的不是太难。在您的onCreate(),设置您的单选按钮用自己的onClick()这样,当他们被激活,他们setChecked(真),并做相反的其他按钮。例如:

                      This really isn't too hard. In your onCreate(), set your RadioButtons with their own onClick() so that when they are activated, they setChecked(true) and do the opposite for the other buttons. For example:

                      class FooActivity {
                      
                          RadioButton m_one, m_two, m_three;
                      
                          @Override
                          protected void onCreate(Bundle savedInstanceState) {
                              ...
                              m_one = (RadioButton) findViewById(R.id.first_radio_button);
                              m_two = (RadioButton) findViewById(R.id.second_radio_button);
                              m_three = (RadioButton) findViewById(R.id.third_radio_button);
                      
                              m_one.setOnClickListener(new OnClickListener() {
                                  public void onClick(View v) {
                                      m_one.setChecked(true);
                                      m_two.setChecked(false);
                                      m_three.setChecked(false);
                                  }
                              });
                      
                              m_two.setOnClickListener(new OnClickListener() {
                                  public void onClick(View v) {
                                      m_one.setChecked(false);
                                      m_two.setChecked(true);
                                      m_three.setChecked(false);
                                  }
                              });
                      
                              m_three.setOnClickListener(new OnClickListener() {
                                  public void onClick(View v) {
                                      m_one.setChecked(false);
                                      m_two.setChecked(false);
                                      m_three.setChecked(true);
                                  }
                              });
                      
                              ...     
                          } // onCreate() 
                      
                      }
                      

                      是的,我知道 - 这样的老派。但是,它的工作原理。祝你好运!

                      Yeah, I know--way old-school. But it works. Good luck!

                      这篇关于如何从不同LinearLayouts组单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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