RadioGroup 自动换行且保留点击事件

简介: RadioGroup 自动换行且保留点击事件

相信用过RadioGroup的同学都踩过很多坑,其中之一就是这个控件设计的不是很合理,不能设置里面的radiobutton的 排列方式(几行几列),导致我们开发的时候要调整里面的布局很是麻烦。


另外一个坑是 动态new 的时候选默认值的问题,这个在之前的一篇文章 RadioGroup中RadioButton默认选中问题  这个里面已经提到过了,就不再细说了。今天主要说说这个radiogroup怎么调整布局为自动换行的问题。


当我们自己写完RadioGroup 后,里面写好radiobutton,非常简单,一通复制粘贴,一大排就出来了,运行一下 ,非常棒,单选等功能都很好用。就像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myradiogroup.MainActivity" >
   <RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="0dp">
        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="红色" />
        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="橙色" />
        <RadioButton
            android:id="@+id/rb_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="黄色" />
        <RadioButton
            android:id="@+id/rb_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绿色" />
        <RadioButton
            android:id="@+id/rb_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="蓝色" />
        <RadioButton
            android:id="@+id/rb_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="靛色" />
        <RadioButton
            android:id="@+id/rb_7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="紫色" />
        <RadioButton
            android:id="@+id/rb_8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="黑色" />
        <RadioButton
            android:id="@+id/rb_9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="灰色" />
        <RadioButton
            android:id="@+id/rb_10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="白色" />
        <RadioButton
            android:id="@+id/rb_11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="七彩色" />
        <RadioButton
            android:id="@+id/rb_12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="粉色" />
    </RadioGroup>
</RelativeLayout>

对应的界面展示效果:

image.png

默认选中了第一个,然后 ,产品经理过来了,你这么放肯定是不行的,能不能改成4行3列的。


然后你心想,小儿科,去查了一下RadioGroup的属性,发现并没有原生支持的属性,可以设置内部RadioButton的几行几列的属性。


继续想一下 ,这还能难道我? 然后写了几个LinearLayout   把RadioButton套起来。


于是代码被你改成了这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myradiogroup.MainActivity" >
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="0dp" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <RadioButton
                android:id="@+id/rb_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="红色" />
            <RadioButton
                android:id="@+id/rb_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="橙色" />
            <RadioButton
                android:id="@+id/rb_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="黄色" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <RadioButton
                android:id="@+id/rb_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="绿色" />
            <RadioButton
                android:id="@+id/rb_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="蓝色" />
            <RadioButton
                android:id="@+id/rb_6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="靛色" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <RadioButton
                android:id="@+id/rb_7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="紫色" />
            <RadioButton
                android:id="@+id/rb_8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="黑色" />
            <RadioButton
                android:id="@+id/rb_9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="灰色" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <RadioButton
                android:id="@+id/rb_10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="白色" />
            <RadioButton
                android:id="@+id/rb_11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="七彩色" />
            <RadioButton
                android:id="@+id/rb_12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="粉色" />
        </LinearLayout>
    </RadioGroup>
</RelativeLayout>

对应的界面展示效果

image.png

完美实现  6不6 ,嘴角浮起了轻蔑的微笑...


然后运行,点击选一下试试,woc,不对啊,这点击效果不对啊 ,发现原来默认选中的 没有取消选中啊,点击事件不对了啊,分分钟懵逼了...


所以其实原生的RadioGroup 存在的问题:


1. 如果不结合其他布局,例如LinearLayout, 则只能实现单行多个按钮组,或者单列多个按钮组。

2. 如果结合其他布局, 虽然可以实现多行多列的RadioButton布局,但是,如果不通过一些互斥算法,也无法实现按钮组的单选操作。


所以要对其进行改写:

package com.example.myradiogroup;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
public class MyRadioGroup extends RadioGroup {
    private static final String TAG = "RadioGroupEx";
    public MyRadioGroup(Context context) {
        super(context);
    }
    public MyRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //调用ViewGroup的方法,测量子view
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        //最大的宽
        int maxWidth = 0;
        //累计的高
        int totalHeight = 0;
        //当前这一行的累计行宽
        int lineWidth = 0;
        //当前这行的最大行高
        int maxLineHeight = 0;
        //用于记录换行前的行宽和行高
        int oldHeight;
        int oldWidth;
        int count = getChildCount();
        //假设 widthMode和heightMode都是AT_MOST
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
            //得到这一行的最高
            oldHeight = maxLineHeight;
            //当前最大宽度
            oldWidth = maxWidth;
            int deltaX = child.getMeasuredWidth() + params.leftMargin + params.rightMargin;
            if (lineWidth + deltaX + getPaddingLeft() + getPaddingRight() > widthSize) {//如果折行,height增加
                //和目前最大的宽度比较,得到最宽。不能加上当前的child的宽,所以用的是oldWidth
                maxWidth = Math.max(lineWidth, oldWidth);
                //重置宽度
                lineWidth = deltaX;
                //累加高度
                totalHeight += oldHeight;
                //重置行高,当前这个View,属于下一行,因此当前最大行高为这个child的高度加上margin
                maxLineHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
                Log.v(TAG, "maxHeight:" + totalHeight + "---" + "maxWidth:" + maxWidth);
            } else {
                //不换行,累加宽度
                lineWidth += deltaX;
                //不换行,计算行最高
                int deltaY = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
                maxLineHeight = Math.max(maxLineHeight, deltaY);
            }
            if (i == count - 1) {
                //前面没有加上下一行的搞,如果是最后一行,还要再叠加上最后一行的最高的值
                totalHeight += maxLineHeight;
                //计算最后一行和前面的最宽的一行比较
                maxWidth = Math.max(lineWidth, oldWidth);
            }
        }
        //加上当前容器的padding值
        maxWidth += getPaddingLeft() + getPaddingRight();
        totalHeight += getPaddingTop() + getPaddingBottom();
        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxWidth,
                heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        //pre为前面所有的child的相加后的位置
        int preLeft = getPaddingLeft();
        int preTop = getPaddingTop();
        //记录每一行的最高值
        int maxHeight = 0;
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
            //r-l为当前容器的宽度。如果子view的累积宽度大于容器宽度,就换行。
            if (preLeft + params.leftMargin + child.getMeasuredWidth() + params.rightMargin + getPaddingRight() > (r - l)) {
                //重置
                preLeft = getPaddingLeft();
                //要选择child的height最大的作为设置
                preTop = preTop + maxHeight;
                maxHeight = getChildAt(i).getMeasuredHeight() + params.topMargin + params.bottomMargin;
            } else { //不换行,计算最大高度
                maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + params.topMargin + params.bottomMargin);
            }
            //left坐标
            int left = preLeft + params.leftMargin;
            //top坐标
            int top = preTop + params.topMargin;
            int right = left + child.getMeasuredWidth();
            int bottom = top + child.getMeasuredHeight();
            //为子view布局
            child.layout(left, top, right, bottom);
            //计算布局结束后,preLeft的值
            preLeft += params.leftMargin + child.getMeasuredWidth() + params.rightMargin;
        }
    }
}

然后将布局文件RadioGroup改为 自定义的MyRadioGroup

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myradiogroup.MainActivity" >
    <com.example.myradiogroup.MyRadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="0dp" >
        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="红色" />
        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="橙色" />
        <RadioButton
            android:id="@+id/rb_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="黄色" />
        <RadioButton
            android:id="@+id/rb_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绿色" />
        <RadioButton
            android:id="@+id/rb_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="蓝色" />
        <RadioButton
            android:id="@+id/rb_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="靛色" />
        <RadioButton
            android:id="@+id/rb_7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="紫色" />
        <RadioButton
            android:id="@+id/rb_8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="黑色" />
        <RadioButton
            android:id="@+id/rb_9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="灰色" />
        <RadioButton
            android:id="@+id/rb_10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="白色" />
        <RadioButton
            android:id="@+id/rb_11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="七彩色" />
        <RadioButton
            android:id="@+id/rb_12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="粉色" />
    </com.example.myradiogroup.MyRadioGroup>
</RelativeLayout>

看下效果和 上面套用linearlayout的效果是一样的,而且不影响RadioGroup的点击事件。


但是这个仅仅是实现了自动换行的效果,没有真正实现可以设置 RadioGroup几行几列的效果。但是可以通过设置RadioButton的宽度就调这个每行的个数,怎么直接在自定义的RadioGroup直接写两个 设置行列的方法,也是没有搞定!如果大家有搞定的 或者有好的方法,可以分享一下!技术就是用来分享的!


相关文章
|
计算机视觉 Python
python基于opencv工具掌纹主线提取
我们将在这篇文章中使用Python和OpenCV库来找出我们手掌中的主线。
|
10月前
|
人工智能 监控 JavaScript
OpenClaw(Clawdbot)云端及本地部署24小时在线AI Agent,全自动炒股票/开发App实战教程
2026年,GitHub上一款开源AI代理框架异军突起——OpenClaw(原Clawdbot)凭借17万+星标成为年度爆款。它彻底打破传统聊天机器人的局限,以“LLM为脑、插件为手”的架构,实现从“聊”到“做”的跨越:既能处理日常办公的邮件收发、日历管理,更能完成全自动炒股、App开发等复杂任务,成为真正的“数字爪子”。
2985 1
|
5月前
|
人工智能 前端开发 小程序
AI 编程工具排名 2026:7 款主流横评(国产 + 海外实测)
AI 编程工具有哪些?哪个 AI 写代码最强?2026 年 AI 编程工具排名横评:Cursor、Bolt.new、Lovable、v0、秒悟 Meoo、通义灵码。从场景适配度、价格、商用合规、中文支持 6 个维度实测。
13917 0
|
Android开发 开发者
Android中弹框如何设计成全屏的
本文介绍在Android中实现全屏对话框的方法,包括使用`Dialog`和`DialogFragment`两种方式。通过设置对话框无标题、调整布局参数及使用透明背景实现全屏效果。适用于希望提升应用交互体验的开发者。
943 0
|
自然语言处理 IDE 开发工具
5分钟完成手势识别项目!CodeBuddy的Craft模式让传统编程方法沦为古董?
本文介绍了使用CodeBuddy快速开发手势识别程序的方法。首先安装Python 3.9.13并配置VS Code环境,接着通过pip安装依赖库`mediapipe`和`opencv-python`。利用CodeBuddy的Craft模式,仅需输入自然语言描述即可生成基础代码,经过简单调整后即可运行。代码实现了四种手势识别(OK、竖大拇指、握拳、张开手掌),并通过摄像头实时展示结果。尽管电脑摄像头像素较低,但识别效果良好。本文旨在帮助读者了解CodeBuddy的强大功能,并激发更多创意应用。
|
JSON API 数据安全/隐私保护
阿里云邮件推送邮件发送失败的问题排查解决
阿里云邮件推送服务中邮件发送失败的排查方法包括:确认SMTP设置正确无误;验证发信域名和邮件地址;检查是否超出发送配额;审查邮件内容以确保合规;确保网络连接稳定;利用发送日志诊断具体问题。当阿里云邮件推送服务出现问题时,可考虑使用AOKSend作为替代方案,其配置简单且服务稳定可靠,支持多种配置选项,并提供详尽的文档支持。示例Python代码展示了如何使用AOKSend API发送邮件。这些步骤有助于确保邮件的顺利发送。
|
安全
工信部ICP备案查询指南
【10月更文挑战第12天】工信部ICP备案查询指南
12045 0
|
存储 缓存 监控
关于redis性能问题分析和优化,看这篇就够了
关于redis性能问题分析和优化,看这篇就够了
2223 0
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8072 2