如何从不同的 LinearLayouts 对 RadioButton 进行分组? [英] How to group RadioButton from different LinearLayouts?

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

问题描述

我想知道是否可以将每个 RadioButton 分组到一个唯一的 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
      • 单选按钮1
      • RadioButton2
      • 单选按钮3

      如您所见,现在每个 RadioButton 都是不同 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:

      • 无线电组
        • LinearLayout_main
          • LinearLayout_1
            • 单选按钮1
            • RadioButton2
            • 单选按钮3

            推荐答案

            Google/Android 的优秀人士似乎认为,当您使用 RadioButtons 时,您不需要 Android 的所有其他方面所带来的灵活性用户界面/布局系统.简单地说:他们不希望您嵌套布局和单选按钮.叹气.

            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() 设置 RadioButtons,以便在激活它们时,它们 setChecked(true) 并对其他按钮执行相反的操作.例如:

            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 对 RadioButton 进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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