第36篇 Android Studio实现点击图片显示信息(三)多行RadioButton的使用

第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(),就可以了。

逻辑如上图。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大唐不良猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值