当前位置: 首页 > news >正文

Android RadioGroup实现多行显示,并保持单选

公司项目最近有个这样的需求,要求实现【多个文本,多行显示,且同时只能选中一个】。设计图效果如下:

看上去很简单,使用 RadioGroup + LinearLayout + RadioButton 快速实现:

<RadioGroupandroid:id="@+id/rg"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="@dimen/SIZE_18"android:layout_marginTop="@dimen/SIZE_9"android:layout_marginRight="@dimen/SIZE_18"android:layout_marginBottom="@dimen/SIZE_20"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/RadioButton1"style="@style/fontStyle"android:layout_width="wrap_content"android:layout_height="28dp"android:background="@drawable/bg_button_bg"android:button="@null"android:paddingLeft="@dimen/SIZE_10"android:paddingRight="@dimen/SIZE_10"android:text="@string/str_abnormal_function"android:textColor="@drawable/text_color_select"android:textSize="@dimen/SP_SIZE_12"></RadioButton><RadioButtonandroid:id="@+id/RadioButton2"android:layout_width="wrap_content"android:layout_height="28dp"android:layout_marginLeft="@dimen/SIZE_10"android:autoSizeMaxTextSize="18sp"android:autoSizeMinTextSize="6sp"android:autoSizeStepGranularity="1sp"android:autoSizeTextType="uniform"android:background="@drawable/bg_button_bg"android:button="@null"android:paddingLeft="@dimen/SIZE_10"android:paddingRight="@dimen/SIZE_10"android:text="@string/str_experience_problem"android:textColor="@drawable/text_color_select"android:textSize="@dimen/SP_SIZE_12"></RadioButton><RadioButtonandroid:id="@+id/RadioButton3"style="@style/fontStyle"android:layout_width="wrap_content"android:layout_height="28dp"android:layout_marginLeft="@dimen/SIZE_10"android:background="@drawable/bg_button_bg"android:button="@null"android:paddingLeft="@dimen/SIZE_10"android:paddingRight="@dimen/SIZE_10"android:text="@string/str_new_feature_suggestion"android:textColor="@drawable/text_color_select"android:textSize="@dimen/SP_SIZE_12"></RadioButton></LinearLayout><RadioButtonandroid:id="@+id/RadioButton4"style="@style/fontStyle"android:layout_width="wrap_content"android:layout_height="28dp"android:layout_marginTop="@dimen/SIZE_9"android:background="@drawable/bg_button_bg"android:button="@null"android:paddingLeft="@dimen/SIZE_10"android:paddingRight="@dimen/SIZE_10"android:text="@string/str_type_other"android:textColor="@drawable/text_color_select"android:textSize="@dimen/SP_SIZE_12"></RadioButton>
</RadioGroup>

跑起来后,发现并没有实现单选的效果,究其根本,观其RadioGroup源码:

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {if (child instanceof RadioButton) {final RadioButton button = (RadioButton) child;if (button.isChecked()) {mProtectFromCheckedChange = true;if (mCheckedId != -1) {setCheckedStateForView(mCheckedId, false);}mProtectFromCheckedChange = false;setCheckedId(button.getId());}}super.addView(child, index, params);
}

RadioGroup在添加子view时,仅判断了其是否是RadioButton,故LinearLayout并不符合这一条件,要想实现内嵌LinearLayout+RadioButton,只能自定义RadioGroup,并重写addView方法。本文使用了另一方式解决这个问题:

使用多个RadioGroup内嵌RadioButton,当其中1个RadioGroup中的RadioButton被选中时,清除其他RadioGroup的选中效果,代码如下:

<RadioGroupandroid:id="@+id/rg"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="@dimen/SIZE_18"android:layout_marginTop="@dimen/SIZE_9"android:layout_marginRight="@dimen/SIZE_18"android:orientation="horizontal"><RadioButtonandroid:id="@+id/RadioButton1"style="@style/fontStyle"android:layout_width="wrap_content"android:layout_height="28dp"android:background="@drawable/bg_button_bg"android:button="@null"android:paddingLeft="@dimen/SIZE_10"android:paddingRight="@dimen/SIZE_10"android:text="@string/str_abnormal_function"android:textColor="@drawable/text_color_select"android:textSize="@dimen/SP_SIZE_12" /><RadioButtonandroid:id="@+id/RadioButton2"android:layout_width="wrap_content"android:layout_height="28dp"android:layout_marginLeft="@dimen/SIZE_10"android:autoSizeMaxTextSize="18sp"android:autoSizeMinTextSize="6sp"android:autoSizeStepGranularity="1sp"android:autoSizeTextType="uniform"android:background="@drawable/bg_button_bg"android:button="@null"android:paddingLeft="@dimen/SIZE_10"android:paddingRight="@dimen/SIZE_10"android:text="@string/str_experience_problem"android:textColor="@drawable/text_color_select"android:textSize="@dimen/SP_SIZE_12" /><RadioButtonandroid:id="@+id/RadioButton3"style="@style/fontStyle"android:layout_width="wrap_content"android:layout_height="28dp"android:layout_marginLeft="@dimen/SIZE_10"android:background="@drawable/bg_button_bg"android:button="@null"android:paddingLeft="@dimen/SIZE_10"android:paddingRight="@dimen/SIZE_10"android:text="@string/str_new_feature_suggestion"android:textColor="@drawable/text_color_select"android:textSize="@dimen/SP_SIZE_12" /></RadioGroup><RadioGroupandroid:id="@+id/rgTow"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="@dimen/SIZE_18"android:layout_marginRight="@dimen/SIZE_18"android:layout_marginBottom="@dimen/SIZE_20"><RadioButtonandroid:id="@+id/RadioButton4"style="@style/fontStyle"android:layout_width="wrap_content"android:layout_height="28dp"android:layout_marginTop="@dimen/SIZE_9"android:background="@drawable/bg_button_bg"android:button="@null"android:paddingLeft="@dimen/SIZE_10"android:paddingRight="@dimen/SIZE_10"android:text="@string/str_type_other"android:textColor="@drawable/text_color_select"android:textSize="@dimen/SP_SIZE_12" />
</RadioGroup>

实现逻辑:

private fun init(){mViewBind.rg.setOnCheckedChangeListener(OneCheckedChange())mViewBind.rgTow.setOnCheckedChangeListener(TowCheckedChange())
}inner class OneCheckedChange : OnCheckedChangeListener {override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {// 清除另一RadioGroup选中状态mViewBind.rgTow.setOnCheckedChangeListener(null)mViewBind.rgTow.clearCheck()mViewBind.rgTow.setOnCheckedChangeListener(TowCheckedChange())// 根据 ID 判断选择的按钮mtype = when (checkedId) {R.id.RadioButton1 -> 1R.id.RadioButton2 -> 2R.id.RadioButton3 -> 3else -> 0}}}inner class TowCheckedChange : OnCheckedChangeListener{override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {mViewBind.rg.setOnCheckedChangeListener(null)mViewBind.rg.clearCheck()mViewBind.rg.setOnCheckedChangeListener(OneCheckedChange())// 根据 ID 判断选择的按钮if(checkedId == R.id.RadioButton4){mtype =4}}}

至此实现【多个文本,多行显示,且同时只能选中一个】效果。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • java中RSA分段加解密及Data must not be longer than异常处理
  • 【海贼王航海日志:前端技术探索】一篇文章带你走进JavaScript(一)
  • JAVA毕业设计635—基于Java+ssm的仓库管理系统(源代码+数据库)
  • 图解Kafka | 彻底弄明白 Kafka 两个最重要的配置
  • NTP时钟同步服务器_ntp时间服务器-京准
  • 简介反向代理作用
  • 基于STM32开发的智能门铃系统
  • “tcp控制协议”的理解
  • 软件测试需要具备的基础知识【功能测试】---后端知识(三)
  • SpringDataElasticsearch在SpringBoot项目中的简单使用
  • 这7款AI网站只用10分钟写万字论文,大学生救星来了吗?
  • 力扣:1456. 定长子串中元音的最大数目
  • 又又又掉毛季 满天飞的浮毛猫毛怎么解决?去浮毛空气净化器推荐
  • PaddleOCR 图片文字提取
  • Flask 异常处理
  • 【笔记】你不知道的JS读书笔记——Promise
  • Github访问慢解决办法
  • Laravel 菜鸟晋级之路
  • PHP变量
  • Vue--数据传输
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 你不可错过的前端面试题(一)
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 树莓派 - 使用须知
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • (BAT向)Java岗常问高频面试汇总:MyBatis 微服务 Spring 分布式 MySQL等(1)
  • (附源码)ssm高校志愿者服务系统 毕业设计 011648
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致
  • (实测可用)(3)Git的使用——RT Thread Stdio添加的软件包,github与gitee冲突造成无法上传文件到gitee
  • (原創) X61用戶,小心你的上蓋!! (NB) (ThinkPad) (X61)
  • (转)linux下的时间函数使用
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • (转)详解PHP处理密码的几种方式
  • ..回顾17,展望18
  • .java 9 找不到符号_java找不到符号
  • .net core docker部署教程和细节问题
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .NET/C# 编译期间能确定的相同字符串,在运行期间是相同的实例
  • .sh
  • .vue文件怎么使用_vue调试工具vue-devtools的安装
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • [ Socket学习 ] 第一章:网络基础知识
  • [ 网络基础篇 ] MAP 迈普交换机常用命令详解
  • [17]JAVAEE-HTTP协议
  • [20150321]索引空块的问题.txt
  • [AI StoryDiffusion] 创造神奇故事,AI漫画大乱斗!
  • [AI]文心一言爆火的同时,ChatGPT带来了这么多的开源项目你了解吗
  • [Bada开发]初步入口函数介绍
  • [C#学习笔记]LINQ
  • [CQOI 2011]动态逆序对