第36篇 Android Studio实现点击图片显示信息(三)多行RadioButton的使用
3.多行RadioButton的使用
3.1.布局文件
<RadioGroup
android:id="@+id/rgp_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbt_student1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:button="@null"
android:layout_weight="1"
android:gravity="center"
android:drawableTop="@mipmap/background_player2"
android:text="@string/student_name_zhao"
android:background="@drawable/radiobutton_background_style"/>
<RadioButton
android:id="@+id/rbt_student2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:button="@null"
android:gravity="center"
android:layout_weight="1"
android:drawableTop="@mipmap/background_player2"
android:text="@string/student_name_wu"
android:background="@drawable/radiobutton_background_style"/>
<RadioButton
android:id="@+id/rbt_student3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:button="@null"
android:layout_weight="1"
android:gravity="center"
android:drawableTop="@mipmap/background_player2"
android:text="@string/student_name_gong"
android:background="@drawable/radiobutton_background_style"/>
</RadioGroup>
<RadioGroup
android:id="@+id/rgp_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbt_student4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:button="@null"
android:layout_weight="1"
android:gravity="center"
android:drawableTop="@mipmap/background_player2"
android:text="@string/student_name_chen"
android:background="@drawable/radiobutton_background_style"/>
<RadioButton
android:id="@+id/rbt_student5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:button="@null"
android:layout_weight="1"
android:gravity="center"
android:drawableTop="@mipmap/background_player2"
android:text="@string/student"
android:background="@drawable/radiobutton_background_style"/>
<RadioButton
android:id="@+id/rbt_student6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:button="@null"
android:layout_weight="1"
android:gravity="center"
android:drawableTop="@mipmap/background_player2"
android:text="@string/student"
android:background="@drawable/radiobutton_background_style"/>
</RadioGroup>
3.2.多行RadioButton
可以看到我们这样布局就有两行的RadioButton,而且是通过两个组实现的,这里一两个问题:
(1)问题1:RadioGroup不能嵌套布局
如果在里面使用线性布局或其他布局的话,那就会使单选按钮自成一家了,没有单选的效果,当然,如果我们没有样式设置(比如你点击背景颜色会变化,那么就需要单选效果,没有变化,那就不需要单选效果了)。所以我的目的是要做到单选背景颜色变化,就需要不嵌套的布局,因此要两个组。
(2)问题2:每个组各管各的,没有单选效果
所以我们要在点击时做一些操作了。
3.3.重写监听事件方法
重写方法:
我们可以看出,这个监听函数传进来两个参数。
第一个参数RadioGroup类型的radioGroup,我们可以通过获取id的方法,判断到底是哪个按钮的单选按钮被选中。
第二个参数就是int类型的i,这个i是一个id,就是当前点击的按钮的id值,可以用来判断点击了哪个按钮。
设置一个全局boolean变量is_select_rgp_one;判断当前点击的组和某一组是不是有冲突,其初始值为true,即默认第一组的按钮是有被选中的。
点击第二组按钮时,如果第一组按钮有被选中的,则清除掉第一组被选中的按钮的选中状态,我们不需要去查哪个按钮被选中,直接适用于clearCheck(),就可以了。
逻辑如上图。