Android 多行RadioGroup 实现

2023-11-05

需求如下:

 思路有多种,可以用自定义布局、RecycleView、代码动态控制布局、RadioGroup 等方式实现;今天我用的RadioGroup ,实现思路如下:

布局文件如下:

 <RadioGroup
                    android:id="@+id/three_rg"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/two_title"
                    android:layout_marginTop="8dp"
                    android:orientation="horizontal">

                    <RadioButton
                        android:id="@+id/btn_1"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="16dp"
                        android:layout_weight="1"
                        android:background="@drawable/radio_select_feedback"
                        android:button="@null"
                        android:checked="true"
                        android:gravity="center"
                        android:text="Task"
                        android:textColor="@drawable/radio_select_feedback_textcolor"
                        android:textSize="12sp" />

                    <RadioButton
                        android:id="@+id/btn_2"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_weight="1"
                        android:background="@drawable/radio_select_feedback"
                        android:button="@null"
                        android:gravity="center"
                        android:text="Invitation"
                        android:textColor="@drawable/radio_select_feedback_textcolor"
                        android:textSize="12sp" />

                    <RadioButton
                        android:id="@+id/btn_3"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="16dp"
                        android:layout_weight="1"
                        android:background="@drawable/radio_select_feedback"
                        android:button="@null"
                        android:gravity="center"
                        android:text="Exchange"
                        android:textColor="@drawable/radio_select_feedback_textcolor"
                        android:textSize="12sp" />
                </RadioGroup>

                <RadioGroup
                    android:id="@+id/four_rg"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/three_rg"
                    android:layout_marginTop="8dp"
                    android:orientation="horizontal">

                    <RadioButton
                        android:id="@+id/btn_4"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="16dp"
                        android:layout_weight="1"
                        android:background="@drawable/radio_select_feedback"
                        android:button="@null"
                        android:checked="false"
                        android:gravity="center"
                        android:text="Sign in"
                        android:textColor="@drawable/radio_select_feedback_textcolor"
                        android:textSize="12sp" />

                    <RadioButton
                        android:id="@+id/btn_5"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_weight="1"
                        android:background="@drawable/radio_select_feedback"
                        android:button="@null"
                        android:gravity="center"
                        android:text="Interface"
                        android:textColor="@drawable/radio_select_feedback_textcolor"
                        android:textSize="12sp" />

                    <RadioButton
                        android:id="@+id/btn_6"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="16dp"
                        android:layout_weight="1"
                        android:background="@drawable/radio_select_feedback"
                        android:button="@null"
                        android:gravity="center"
                        android:text="Other"
                        android:textColor="@drawable/radio_select_feedback_textcolor"
                        android:textSize="12sp" />
                </RadioGroup>

radio_select_feedback.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/c_14_ffa827"></item>
    <item android:state_checked="false" android:drawable="@drawable/c_14_f2f2f2"></item>
</selector>

radio_select_feedback_textcolor.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/ffa827"></item>
    <item android:state_checked="false" android:color="@color/f999999"></item>
</selector>

主控制代码实现:

1.//初始化代码
threeRg.clearCheck(); // this is so we can start fresh, with no selection on both         RadioGroups
fourRg.clearCheck();
threeRg.setOnCheckedChangeListener(listener1);
fourRg.setOnCheckedChangeListener(listener2);




2.//内部Listener

 private RadioGroup.OnCheckedChangeListener listener1 = new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId != -1) {
                fourRg.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
                fourRg.clearCheck(); // clear the second RadioGroup!
                fourRg.setOnCheckedChangeListener(listener2); //reset the listener
            }
        }
    };

    private RadioGroup.OnCheckedChangeListener listener2 = new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId != -1) {
                threeRg.setOnCheckedChangeListener(null);
                threeRg.clearCheck();
                threeRg.setOnCheckedChangeListener(listener1);
            }
        }
    };

3.使用
threeRg.getCheckedRadioButtonId() == -1  代表第一行没有选中的
fourRg.getCheckedRadioButtonId() == -1   代表第二行也没有选中的
选中的话是有对应ID来匹配;

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 多行RadioGroup 实现 的相关文章

随机推荐