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

news2024/9/21 22:48:08

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

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

<RadioGroup
	android: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">

	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:orientation="horizontal">

		<RadioButton
			android: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>

		<RadioButton
			android: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>

		<RadioButton
			android: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>

	<RadioButton
		android: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的选中效果,代码如下:

<RadioGroup
	android: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">

	<RadioButton
		android: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
		android: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
		android: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>

<RadioGroup
	android: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">

	<RadioButton
		android: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 -> 1
			R.id.RadioButton2 -> 2
			R.id.RadioButton3 -> 3
			else -> 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
		}
	}

}

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1990455.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

项目细节优化

文章目录 1.解决重复注册1.AuthUserDomainServiceImpl.java 在注册之前从db查询是否已经存在该用户2.AuthUserService.java3.AuthUserServiceImpl.java4.测试1.请求2.后台&#xff0c;提示已经注册 2.用户信息查询1.接口设计2.代码实现1.UserController.java2.AuthUserDomainSe…

C语言:复读机2种写法(输入什么就输出什么)

&#xff08;1&#xff09;题目&#xff1a;输入什么内容&#xff0c;输出就是什么内容&#xff0c;遇到"#"为止。输入一个随便的字符 &#xff08;2&#xff09;代码&#xff1a; 【1】getchar()和putchar() #include "stdio.h"int main() {char ch;pr…

基于Python的Scrapy爬虫的个性化书籍推荐系统【Django框架、超详细系统设计原型】

文章目录 有需要本项目的代码或文档以及全部资源&#xff0c;或者部署调试可以私信博主项目介绍系统分析系统设计展示总结 有需要本项目的代码或文档以及全部资源&#xff0c;或者部署调试可以私信博主 项目介绍 近年来&#xff0c;随着互联网的蓬勃发展&#xff0c;企事业单…

linux docker 仓库拉取失败问题(已解决)

仓库拉取失败问题 前言错误示例解决注意 前言 我在服务器拉取仓库的镜像时&#xff0c;出现如下错误&#xff1a; Error response from daemon: Get “http://192.168.37.51:8050/v2/”: net/http: HTTP/1.x transport connection broken: malformed HTTP response “\x15\x0…

数据结构_study(七)

查找 查找表&#xff1a;同一类型的数据元素构成的集合 关键字&#xff08;键值&#xff09;&#xff1a;数据元素中某个数据项的值&#xff0c;用来标识数据元素 主关键字&#xff1a;唯一标识一个记录的关键字 次关键字&#xff1a;可以标识多个数据元素的关键字 查找&…

浅析DNS服务器:办公网DNS的架构思路分享

办公网 DNS 不同于业务网 DNS&#xff0c;主要承担的是企业或组织机构内部员工的日常办公的域名解析需求。比如日常的办公系统的访问、通过第三方认证系统进行身份认证才能访问办公系统资源、办公PC需要进行AD域控管理等……由于此办公网DNS服务器的设计需求也不尽相同&#xf…

【脚本说明撰写markdown】如何基于VScode 撰写使用说明文档,及格式转换.md、.html、.pdf格式

如何基于VScode 撰写使用说明文档&#xff0c;及格式转换.md、.html、.pdf格式 下载插件 下载markdown 进入vscode后&#xff0c;进入扩展工具&#xff08;快捷键ctrlshiftX&#xff09;&#xff0c;搜索markdown&#xff0c;选择markdown All in One并安装 安装Markdown Pr…

SolidWorks钣金中如何定义K因子、折弯系数

在使用SolidWorks设计钣金零件时总是会遇到折弯系数、K因子这样的专业名称&#xff0c;不知如何去定义他们的。 在SolidWorks中除了直接指定K因子确定折弯系数之外还可以利用折弯系数表来确定。在折弯系数表中指定钣金零件的折弯系数或折弯扣除数值等&#xff0c;折弯系数表还…

【Material-UI】按钮组:Split Button 详解

文章目录 一、Split Button 概述1. 组件介绍2. 基本用法 二、Split Button 的应用场景1. 提交操作2. 导出操作3. 文件操作 三、Split Button 的样式定制1. 变体&#xff08;Variants&#xff09;2. 颜色&#xff08;Colors&#xff09; 四、Split Button 的优势1. 提升用户体验…

江科大/江协科技 STM32学习笔记P20

文章目录 编码器接口测速定时器有关的库函数Encoder.cmain.c 编码器接口测速 编码器接口的初始化&#xff0c;第一步&#xff0c;RCC开启时钟&#xff0c;开启GPIO和定时器的时钟&#xff0c;第二步&#xff0c;配置GPIO&#xff0c;这里把PA6和PA7配置成输入模式&#xff0c;第…

【环绕字符串中唯一的子字符串】python刷题记录

R4-字符串 动态规划 class Solution:def findSubstringInWraproundString(self, s: str) -> int:dp[0]*26num1#dp初始化dp[ord(s[0])-ord(a)]1for c1,c2 in pairwise(s):if not (ord(c2)-ord(c1)-1)%26:num1else:num1dp[id]max(dp[id : ord(c2)-ord(a)],num)return sum(dp)p…

Java设计模式(适配器模式)

定义 将一个类的接口转换成客户希望的另一个接口。适配器模式让那些接口不兼容的类可以一起工作。 角色 目标抽象类&#xff08;Target&#xff09;&#xff1a;目标抽象类定义客户所需的接口&#xff08;在类适配器中&#xff0c;目标抽象类只能是接口&#xff09;。 适配器类…

笔记小结:《利用Python进行数据分析》之字符串操作(含正则表达式)

目录 字符串对象方法 使用split分割字符串 连接字符串 查找子串 字串计数 替换字串 字符串方法表 正则表达式 分割数量不定的空白符 匹配正则表达式的所有模式 匹配字符串 替换字符串 将字符串分组 带有分组功能的findall pandas矢量化字符串函数 Python能够成…

hongmeng开发

Image图片组件 Text组件 如在两个限定目录里面定义完后&#xff0c;也要在 base牡蛎下去定义一下&#xff0c;不然会报错 TextInput Button Slider 案例 State imageWidth:number30//构建 → 界面build() {Column(){Image($r(app.media.startIcon)).width(this.imageWidth)Te…

帆软填报报表单元格根据其它单元格内容决定另外的单元格可筛选什么值

效果图&#xff1a; 方法有三种&#xff1a; 方法一&#xff1a; 添加链接描述

基于sklearn的机器学习 — 支持向量机(SVM)

支持向量机&#xff08;SVM&#xff1a;support vector machine&#xff09;另一种功能强大、应用广泛的学习算法&#xff0c;可应用于分类、回归、密度估计、聚类等问题。SVM可以看作是感知器&#xff08;可被视为一种最简单形式的前馈神经网络&#xff0c;是一种二元线性分类…

AI在医学领域:使用眼底图像和基线屈光数据来定量预测近视

关键词&#xff1a;深度学习、近视预测、早期干预、屈光数据 儿童近视已经成为一个全球性的重大健康议题。其发病率持续攀升&#xff0c;且有可能演变成严重且不可逆转的状况&#xff0c;这不仅对家庭幸福构成威胁&#xff0c;还带来巨大的经济负担。当前的研究着重指出&#x…

Android Studio新版UI常用设置

新版UI固然好看&#xff0c;但启用后一些常用的功能也被初始化了&#xff0c;下面会说明如何设置一些常用的功能。 一、启用\禁用新版UI Setting -> Appearance & Behavior -> New UI -> Enable new UI 二、展示Git部分的Local Changes窗口 Setting -> Ve…

Leetcode JAVA刷刷站(1)两数之和

一、题目概述 二、思路方向 为了解决这个问题&#xff0c;你可以使用哈希表&#xff08;在Java中通常使用HashMap&#xff09;来存储遍历过的数组元素及其对应的索引。这样&#xff0c;当你遍历数组时&#xff0c;你可以检查target - 当前元素是否已经在哈希表中&#xff0c;如…

SpringBoot(Ⅰ)——HelloWorld和基本打包部署+Pom依赖概述+@SpringBootApplication注解+自动装配原理

前言 如果SSM学的比较好&#xff0c;那么SpringBoot说白了就两件事:约定大于配置和自动装配 SpringBoot不会提供任何的功能拓展&#xff0c;完全依赖我们手动添加 所以SpringBoot的本质是一个依赖脚手架&#xff0c;可以快速集成配置各种依赖 1.1 SpringBoot相关依赖 创建…