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

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景