Android实现RadioGroup之间的互斥
关于
因为一个页面需求,需要有两个radio group共六个radio button来实现单选互斥(为什么不用一个radio group来包裹是因为需要两行展示)
效果图
实现
准备工作,附上布局代码
首先修改activity_radio_group.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RadioGroupActivity">
<RadioGroup
android:id="@+id/rgFlowSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:foreground="?android:attr/selectableItemBackground"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="horizontal">
<RadioButton
android:id="@+id/tvSwitchFace"
android:layout_width="64dp"
android:layout_height="68dp"
android:background="@drawable/selector_air_flow"
android:gravity="center"
android:layout_weight="1"
android:layout_marginStart="48dp"
android:button="@null"
/>
<RadioButton
android:id="@+id/tvSwitchFaceFeet"
android:layout_width="64dp"
android:layout_height="68dp"