android自定义RadioGroup实现可以添加多种布局

2024-02-26 06:32

本文主要是介绍android自定义RadioGroup实现可以添加多种布局,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载自

 

android自带的RadioGroup是继承自LinearLayout,如果布局的时候不是直接写radiobutton,即radiobutton外面还包了一层容器,这时分组是不成功的,因为查找不到radiobutton,如果要实现这种效果呢,于是看了RadioGroup的源码,发现问题在于addView方法和自定义的PassThroughHierarchyChangeListener;

下面就这两个地方动手脚,先拷贝源码,然后去掉RadioGroup(Context context, AttributeSet attrs) 中的方法,我新增了一个方法,查找viewGroup控件中的radioButton,

复制代码
/** 查找radioButton控件 */public RadioButton findRadioButton(ViewGroup group) {RadioButton resBtn = null;int len = group.getChildCount();for (int i = 0; i < len; i++) {if (group.getChildAt(i) instanceof RadioButton) {resBtn = (RadioButton) group.getChildAt(i);} else if (group.getChildAt(i) instanceof ViewGroup) {findRadioButton((ViewGroup) group.getChildAt(i));}}return resBtn;}
复制代码

addView的实现如下:

复制代码
 1 @Override
 2     public void addView(View child, int index, ViewGroup.LayoutParams params) {
 3         if (child instanceof RadioButton) {
 4             final RadioButton button = (RadioButton) child;
 5             if (button.isChecked()) {
 6                 mProtectFromCheckedChange = true;
 7                 if (mCheckedId != -1) {
 8                     setCheckedStateForView(mCheckedId, false);
 9                 }
10                 mProtectFromCheckedChange = false;
11                 setCheckedId(button.getId());
12             }
13         } else if (child instanceof ViewGroup) {  //这里是我添加的代码
14             final RadioButton button = findRadioButton((ViewGroup) child);
15             if (button.isChecked()) {
16                 mProtectFromCheckedChange = true;
17                 if (mCheckedId != -1) {
18                     setCheckedStateForView(mCheckedId, false);
19                 }
20                 mProtectFromCheckedChange = false;
21                 setCheckedId(button.getId());
22             }
23         }
24 
25         super.addView(child, index, params);
26     }
复制代码

然后在自定义的PassThroughHierarchyChangeListener中修改:

复制代码
private class PassThroughHierarchyChangeListener implementsViewGroup.OnHierarchyChangeListener {private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener;public void onChildViewAdded(View parent, View child) {if (parent == MyRadioGroup.this && child instanceof RadioButton) {int id = child.getId();// generates an id if it's missingif (id == View.NO_ID) {id = child.hashCode();child.setId(id);}((RadioButton) child).setOnCheckedChangeListener(mChildOnCheckedChangeListener);} else if (parent == MyRadioGroup.this   //这里是我添加的代码&& child instanceof ViewGroup) {RadioButton btn = findRadioButton((ViewGroup) child);int id = btn.getId();// generates an id if it's missingif (id == View.NO_ID) {id = btn.hashCode();btn.setId(id);}btn.setOnCheckedChangeListener(mChildOnCheckedChangeListener);}if (mOnHierarchyChangeListener != null) {mOnHierarchyChangeListener.onChildViewAdded(parent, child);}}public void onChildViewRemoved(View parent, View child) {if (parent == MyRadioGroup.this && child instanceof RadioButton) {((RadioButton) child).setOnCheckedChangeListener(null);} else if (parent == MyRadioGroup.this&& child instanceof ViewGroup) {findRadioButton((ViewGroup) child).setOnCheckedChangeListener(null);}if (mOnHierarchyChangeListener != null) {mOnHierarchyChangeListener.onChildViewRemoved(parent, child);}}}
复制代码

做好了上面的步骤,下面布局就可以按照自己的意思来了,并且分组效果也不会影响

下面我的布局文件demo:

复制代码
 <com.huaheng.wy.view.MyRadioGroupandroid:id="@+id/rg"android:layout_width="fill_parent"android:layout_height="65dp"android:layout_alignParentBottom="true"android:background="@drawable/yst_i_bottom"android:orientation="horizontal" ><!-- 首页 --><FrameLayoutandroid:id="@+id/left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginBottom="2dp"android:layout_marginTop="3dp"android:layout_weight="1" ><RadioButtonandroid:id="@+id/rbtn_home"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@null"android:button="@null"android:drawableTop="@drawable/yst_i_bottomhome_selector"android:gravity="center_horizontal"android:text="首页"android:textColor="@color/white"android:textSize="@dimen/font_5" /><Buttonandroid:id="@+id/btn_home_point"android:layout_width="21dp"android:layout_height="20dp"android:layout_gravity="top|right"android:layout_marginRight="10dp"android:layout_marginTop="0dp"android:background="@drawable/notice_bg"android:text="1"android:textColor="@color/white"android:textSize="@dimen/font_6"android:visibility="gone" /></FrameLayout><!-- 历史 --><RadioButtonandroid:id="@+id/rbtn_his"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginBottom="2dp"android:layout_marginTop="3dp"android:layout_weight="1"android:background="@null"android:button="@null"android:drawableTop="@drawable/yst_i_bottomhis_selector"android:gravity="center_horizontal"android:text="历史"android:textColor="@color/white"android:textSize="@dimen/font_5" /><!-- 扫描 --><Buttonandroid:id="@+id/rbtn_scan"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:background="@drawable/scan_selector"android:button="@null"android:gravity="center_horizontal|bottom"android:paddingBottom="7dp"android:text="扫描"android:textColor="@color/white"android:textSize="@dimen/font_5" /><RadioButtonandroid:id="@+id/rbtn_seek"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginBottom="2dp"android:layout_marginTop="3dp"android:layout_weight="1"android:background="@null"android:button="@null"android:drawableTop="@drawable/yst_i_bottomseek_selector"android:gravity="center_horizontal"android:text="搜索"android:textColor="@color/white"android:textSize="@dimen/font_5" /><RadioButtonandroid:id="@+id/rbtn_more"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginBottom="2dp"android:layout_marginTop="3dp"android:layout_weight="1"android:background="@null"android:button="@null"android:drawableTop="@drawable/yst_i_bottommore_selector"android:gravity="center_horizontal"android:text="更多"android:textColor="@color/white"android:textSize="@dimen/font_5" /></com.huaheng.wy.view.MyRadioGroup>

这篇关于android自定义RadioGroup实现可以添加多种布局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/747943

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Nginx安全防护的多种方法

《Nginx安全防护的多种方法》在生产环境中,需要隐藏Nginx的版本号,以避免泄漏Nginx的版本,使攻击者不能针对特定版本进行攻击,下面就来介绍一下Nginx安全防护的方法,感兴趣的可以了解一下... 目录核心安全配置1.编译安装 Nginx2.隐藏版本号3.限制危险请求方法4.请求限制(CC攻击防御)

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录