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"><RadioGroupandroid: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"><RadioButtonandroid: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"/><RadioButtonandroid:id="@+id/tvSwitchFaceFeet"android:layout_width="64dp"android:layout_height="68dp"android:background="@drawable/selector_air_flow"android:checked="false"android:layout_marginStart="48dp"android:button="@null"android:layout_weight="1"android:gravity="center"/><RadioButtonandroid:id="@+id/tvSwitchBalance"android:layout_width="64dp"android:layout_height="68dp"android:layout_marginStart="48dp"android:background="@drawable/selector_air_flow"android:checked="false"android:layout_marginEnd="39dp"android:layout_weight="1"android:button="@null"android:gravity="center"/></RadioGroup><RadioGroupandroid:id="@+id/rgFlowTwoSwitch"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_toBottomOf="@+id/rgFlowSwitch"android:orientation="horizontal"><RadioButtonandroid:id="@+id/tvSwitchFeet"android:layout_width="64dp"android:layout_height="68dp"android:background="@drawable/selector_air_flow"android:checked="false"android:layout_marginStart="48dp"android:button="@null"android:layout_weight="1"android:gravity="center"/><RadioButtonandroid:id="@+id/tvSwitchFeetWindow"android:layout_width="64dp"android:layout_height="68dp"android:background="@drawable/selector_air_flow"android:checked="false"android:layout_marginStart="48dp"android:button="@null"android:layout_weight="1"android:gravity="center"/><RadioButtonandroid:id="@+id/tvSwitchWindow"android:layout_width="64dp"android:layout_height="68dp"android:background="@drawable/selector_air_flow"android:checked="false"android:layout_marginStart="48dp"android:layout_marginEnd="39dp"android:button="@null"android:layout_weight="1"android:gravity="center"/></RadioGroup></androidx.constraintlayout.widget.ConstraintLayout>

  贴一下对应radio button的背景selector_air_flow.xml:

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

  再贴一下对应bg_air_flow_circle_blue_full.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval"xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#0099ff" /><corners android:radius="3dp" /><sizeandroid:width="40dp"android:height="40dp" /><paddingandroid:bottom="7dp"android:left="7dp"android:right="7dp"android:top="7dp" />
</shape>

  最后贴一下bg_radio_circle_gray_03.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><corners android:radius="3dp" /><solidandroid:color="@color/transparent"/><sizeandroid:width="40dp"android:height="40dp" /><paddingandroid:bottom="7dp"android:left="7dp"android:right="7dp"android:top="7dp" /><strokeandroid:width="0.6dp"android:color="@color/black"/>
</shape>

  好了,准备工作完成

解决需求,附上类代码

 private lateinit var viewBinding: ActivityRadioGroupBindingoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)viewBinding = ActivityRadioGroupBinding.inflate(layoutInflater)setContentView(viewBinding.root)viewBinding.rgFlowSwitch.setOnCheckedChangeListener { group, checkedId ->when(checkedId){//check(-1)表示选择null,方法等价.clearCheck()R.id.tvSwitchFace -> viewBinding.rgFlowTwoSwitch.check(-1)R.id.tvSwitchFaceFeet -> viewBinding.rgFlowTwoSwitch.check(-1)R.id.tvSwitchBalance -> viewBinding.rgFlowTwoSwitch.check(-1)}}viewBinding.tvSwitchFeet.setOnCheckedChangeListener { _, isChecked ->if (isChecked) viewBinding.rgFlowSwitch.check(-1)}viewBinding.tvSwitchFeetWindow.setOnCheckedChangeListener { _, isChecked ->if (isChecked) viewBinding.rgFlowSwitch.check(-1)}viewBinding.tvSwitchWindow.setOnCheckedChangeListener { _, isChecked ->if (isChecked) viewBinding.rgFlowSwitch.check(-1)}//因为group的OnCheckedChange会监听子button的状态变化,所以也会服用导致死循环,所以两个group的checked监听不可,超过两个以上的radiogroup就要采用group的onchecked监听+其余group的radiobutton的onchecked监听/* viewBinding.rgFlowTwoSwitch.setOnCheckedChangeListener { group, checkedId ->if (group.checkedRadioButtonId !=-1){when(checkedId){R.id.tvSwitchFeet -> viewBinding.rgFlowSwitch.check(-1)R.id.tvSwitchWindow -> viewBinding.rgFlowSwitch.check(-1)R.id.tvSwitchFeetWindow -> viewBinding.rgFlowSwitch.check(-1)}}}*/}

  本篇用来记录解决问题的过程,很多测试探索的步骤就不在这里展示,有问题欢迎批评指正,觉得不错的也请点个赞,多谢
  进阶篇《Android实现RadioGroup之间的互斥且radioButton可以选择或取消》

Android实现RadioGroup之间的互斥相关推荐

  1. Android 多组RadioGroup的选项互斥

    在开发中,也会遇到两组RadioGroup选择项互斥的需求,其实也就是清除另外一组的选项 mRadioGroup.setOnCheckedChangeListener(new RadioGroup.O ...

  2. Android 应用程序之间内容分享详解(二)

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/9428613 Android 应用程序之间内容分享详解(一) 之前给大家分享了你开发的应 ...

  3. 大叔也说Xamarin~Android篇~Activity之间传递数组

    大叔也说Xamarin~Android篇~Activity之间传递数组 原文:大叔也说Xamarin~Android篇~Activity之间传递数组 我们在开发应用程序时,不可能只使用一个Layout ...

  4. 三个activity之间跳转 数据传递_第二百四十二回:Android中Fragment之间的数据传递概述...

    各位看官们大家好,上一回中咱们说的是Android中Activity之间数据传递的例子,这一回咱们说的例子是Fragment之间的数据传递.闲话休提,言归正转.让我们一起Talk Android吧! ...

  5. Android的intent之间复杂参数的传递

    2019独角兽企业重金招聘Python工程师标准>>> Intent是Activity与Activity之间,Activity与Service之间传递参数的介质 Intent传递的参 ...

  6. Android应用程序之间共享文字和图片(一)

    以下为TestReceiveShare1工程 MainActivity如下: package cn.testreceiveshare1; import java.util.ArrayList; imp ...

  7. android 打印流程图,Android实现Activities之间进行数据传递的方法

    本文实例讲述了Android实现Activities之间进行数据传递的方法.分享给大家供大家参考.具体分析如下: 首先,先说明一下Activity的启动及关闭: 1. startActivity(In ...

  8. Android 和 PHP 之间进行数据加密传输

    Android 和 PHP 之间进行数据加密传输 [代码] [Java]代码 1 mcrypt = new MCrypt(); 2 /* Encrypt */ 3 String encrypted = ...

  9. Android在片段之间传递数据

    In this tutorial, we'll be developing an application that contains TabLayout, ViewPager and Fragment ...

最新文章

  1. 经典C语言程序100例之七二
  2. Java锤子剪刀布大家应该都会玩“锤子剪刀布”的游戏: 现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
  3. pandas输出到excel_精通Pandas,从零基础到中级运用,看这一篇就够了
  4. Android 10.0修改系统型号(Build.MODEL)
  5. 微信小程序之扫普通链接二维码打开小程序实现动态传递参数及踩坑总结
  6. linux 股票指南针,Android 利用方向传感器实现 指南针
  7. Vue打包优化篇-CDN加速
  8. Android无障碍服务( Accessibility Service)应用
  9. 获取URL地址时某些参数被转义
  10. [人脸算法]技术方向综述
  11. 5G NR 缩略语整理
  12. ESP8266NodeMcu连接不上WIFI解决方法
  13. 30天自制操作系统【笔记】
  14. python 10行代码生成词云图片(基础词云、形状词云)
  15. python中的pass是什么意思_Python中pass的作用与使用教程
  16. updater-script命令详解教你写刷机脚本
  17. OCR开源库Tesseract汉字识别训练
  18. mysql定时任务每天凌晨三点钟醒来_常常凌晨三四点醒来是怎么回事?遇到这事要警惕了...
  19. Matlab中有与、 或、 异或的操作
  20. RedHat Linux口令恢复任我行(转)

热门文章

  1. Oracle 截取字符串substr函数使用方法
  2. 线性回归-最小二乘法
  3. 2022-5月-第一周日报
  4. C/C++中二维数组作为函数参数------方法及要点讲解
  5. 三星N148上网本上安装windows 2000
  6. brt考试计算机成绩,BRT车道优化控制系统-计算机系本科论文开题报告
  7. python爬虫系列(2)—— requests和BeautifulSoup
  8. css如何设置高亮显示,Javascript实现CSS代码高亮显示
  9. 有了QQ为什么微信还会火?
  10. am335x otg配置