需求如下:
![](https://img-blog.csdnimg.cn/153e516ef7db468c9326341fda70eb59.png)
思路有多种,可以用自定义布局、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来匹配;