java - 如何将线性布局中的单选按钮放入 RadioGroup 中

标签 java android xml android-layout android-studio

我有六个属于同一问题的 RadioButton,我需要将它们放入某个 RadioGroup 中。但这些RadioButtons是在一些LinearLayouts中。我尝试为每个 RadioButton 放置一个 RadioGroup,但我不知道需要做什么才能使其工作。

enter image description here

tab_layout_a.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_gravity="bottom"
    android:layout_weight="1"
    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="horizontal">

                            <RadioButton
                                android:id="@+id/tipoPackRadio1"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:layout_weight="1"
                                android:text="Normal"
                                android:textSize="@dimen/radiobuttons_font_sz" />

                            <RadioButton
                                android:id="@+id/tipoPackRadio2"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:layout_marginLeft="5dp"
                                android:layout_marginRight="5dp"
                                android:layout_weight="1"
                                android:text="Gilda"
                                android:textSize="@dimen/radiobuttons_font_sz" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            android:layout_weight="0.9"
                            android:orientation="horizontal">

                                <RadioButton
                                    android:id="@+id/tipoPackRadio3"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center"
                                    android:layout_marginLeft="0dp"
                                    android:layout_weight="1"
                                    android:text="Aged Pack \n(cheese/salve)"
                                    android:textSize="@dimen/radiobuttons_font_sz" />

                        </LinearLayout>

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="bottom"
                        android:layout_weight="1"
                        android:orientation="horizontal">


                            <RadioButton
                                android:id="@+id/tipoPackRadio4"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:layout_marginRight="0dp"
                                android:layout_weight="1"
                                android:text="Fellowship"
                                android:textSize="@dimen/radiobuttons_font_sz" />

                            <RadioButton
                                android:id="@+id/tipoPackRadio5"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:layout_marginLeft="5dp"
                                android:layout_marginRight="5dp"
                                android:layout_weight="1"
                                android:text="Fertilizer"
                                android:textSize="@dimen/radiobuttons_font_sz" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="0.9"
                            android:orientation="horizontal">

                                <RadioButton
                                    android:id="@+id/tipoPackRadio6"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center"
                                    android:layout_marginLeft="0dp"
                                    android:layout_weight="1"
                                    android:text="Aged Pack \n(honey)"
                                    android:textSize="@dimen/radiobuttons_font_sz"
                                    android:visibility="visible" />

                        </LinearLayout>
                    </LinearLayout>

                </LinearLayout>

最佳答案

尝试这个解决方案..

   class MyActivity extends Activity {

RadioButton _Rone, _Rtwo, _Rthree;

@Override
protected void onCreate(Bundle savedInstanceState) {

//after setContentView(.....)

    _Rone = (RadioButton) findViewById(R.id.radio_one);
    _Rtwo = (RadioButton) findViewById(R.id.radio_two);
    _Rthree = (RadioButton) findViewById(R.id.radio_three);

    _Rone.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            _Rone.setChecked(true);
            _Rtwo.setChecked(false);
            _Rthree.setChecked(false);
        }
    });

    _Rtwo.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            _Rone.setChecked(false);
            _Rtwo.setChecked(true);
            _Rthree.setChecked(false);
        }
    });

    _Rthree.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            _Rone.setChecked(false);
            _Rtwo.setChecked(false);
            _Rthree.setChecked(true);
        }
    });
 // do code here
}`

通过单击任何单选按钮,其他人的选择将重置。

关于java - 如何将线性布局中的单选按钮放入 RadioGroup 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43311905/

相关文章:

java - Play Framework - 值登录不是 controllers.Application 的成员

java - 不幸的是,当我定义 onItemSelectedListener 时出现停止错误

android - 检测当前正在运行的应用程序。安卓

java - 向实现者隐藏库包

java - 应用内购买将交易收据发送到服务器

android - 从全屏 Activity 开始新 Activity 会导致状态栏覆盖操作栏

xml - XSD:根据先前属性的值定义属性

android - 在android中创建水平和垂直虚线

javascript - jQuery表单插件和PHP文件上传教程问题

java - Selenium Grid并行测试时如何让hub决定执行哪个节点?