自定义支持多行显示的RadioGroup

原生的RadioGroup继承自LinearLayout,即只能支持一横排或者一竖排的排列显示RadioButton

现在改写RadioGroup,使它支持多行多列排布RadioButton,效果图如下

效果图

重写RadioGroup

package com.kongqw.radiogroupdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup; /**
* Created by kongqw on 2016/2/18.
*/
public class MyRadioGroup extends RadioGroup { private OnCheckedChangeListener mOnCheckedChangeListener; public MyRadioGroup(Context context) {
super(context);
} public MyRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
} public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
} @Override
public void addView(final View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof LinearLayout) {
int childCount = ((LinearLayout) child).getChildCount();
for (int i = 0; i < childCount; i++) {
View view = ((LinearLayout) child).getChildAt(i);
if (view instanceof RadioButton) {
final RadioButton button = (RadioButton) view;
((RadioButton) button).setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
((RadioButton) button).setChecked(true);
checkRadioButton((RadioButton) button);
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(MyRadioGroup.this, button.getId());
}
return true;
}
});
}
}
} super.addView(child, index, params);
} private void checkRadioButton(RadioButton radioButton) {
View child;
int radioCount = getChildCount();
for (int i = 0; i < radioCount; i++) {
child = getChildAt(i);
if (child instanceof RadioButton) {
if (child == radioButton) {
// do nothing
} else {
((RadioButton) child).setChecked(false);
}
} else if (child instanceof LinearLayout) {
int childCount = ((LinearLayout) child).getChildCount();
for (int j = 0; j < childCount; j++) {
View view = ((LinearLayout) child).getChildAt(j);
if (view instanceof RadioButton) {
final RadioButton button = (RadioButton) view;
if (button == radioButton) {
// do nothing
} else {
((RadioButton) button).setChecked(false);
}
}
}
}
}
}
}

布局

RadioGroup里面还可以包裹一层LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<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.kongqw.radiogroupdemo.MainActivity"> <com.kongqw.radiogroupdemo.MyRadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OC" />
</LinearLayout>
</com.kongqw.radiogroupdemo.MyRadioGroup>
</RelativeLayout>

自定义支持多行显示的RadioGroup的更多相关文章

  1. 简单的自绘CListBox(多行显示)(覆盖DrawItem函数,然后用CDC绘制)

    之前写过一个自绘的CListBox类,详细请参考http://blog.csdn.net/VisualEleven/archive/2010/10/12/5935430.aspx现在修改这之前的代码, ...

  2. Android的有关EditText的能多行显示但无法禁止自动换行的Bug!

    需求: 使 EditText或TextView 支持 多行显示,但是不自动换行,即能水平滚动较长的内容. Bug: 想当然的,在XML定义中设置如下,应该就可以了. android:scrollHor ...

  3. HTML5 元素文字超出部分显示省略号(支持多行),兼容几乎所有常用浏览器

    1,公共样式,在公共的 CSS 文件中加入以下内容  /* 超出部分显示省略号,支持多行 */ .text-ells:before { content: ''; float: left; width: ...

  4. 写了一个迷你toast提示插件,支持自定义提示文字和显示时间

    写了一个迷你toast提示插件,支持自定义提示文字和显示时间,不想用其他第三方的ui插件,又想要toast等小效果来完善交互的同学可以试试, 代码中还贡献了一段css能力检测js工具函数,做项目的时候 ...

  5. MYSQL 命令行显示乱码 解决方案

    中文乱码是因为编码集不支持,所以要改变编码 先查看下设置的编码 使用如下命令 show variables like 'character%'; 在 mysql.conf (Ubuntu mysql5 ...

  6. Android中的TextView实现多行显示省略号

    今天遇到一个问题就是TextView显示内容的时候,多行显示的时候,显示省略号的问题,刚开始没有找到一个好的办法,只找到一个自定义TextView组件的方法,然而今天在贴吧中找到一个更好,更简便的方法 ...

  7. Android 自定义支持快速搜索筛选的选择控件(一)

    Android 自定义支持快速搜索筛选的选择控件 项目中遇到选择控件选项过多,需要快速查找匹配的情况. 做了简单的Demo,效果图如下: 源码地址:https://github.com/whieenz ...

  8. 【WPF】ListBox GridViewColumn Header 文字换行、文字多行显示

    ListBox GridViewColumn Header 文字换行.文字多行显示,在Header中需要换行的地方写 <GridViewColumn Header="空间另存 为总量& ...

  9. jquery.ellipsis根据宽度(不是字数)进行内容截断,支持多行内容

    jquery.ellipsis 自动计算内容宽度(不是字数)截断,并加上省略号,内容不受中英文或符号限制. 如果根据字数来计算的话,因为不同字符的宽度并不相同,比如l和W,特别是中英文,最终内容宽度会 ...

随机推荐

  1. Web SCADA 电力接线图工控组态编辑器

    前言 SVG并非仅仅是一种图像格式, 由于它是一种基于XML的语言,也就意味着它继承了XML的跨平台性和可扩展性,从而在图形可重用性上迈出了一大步.如SVG可以内嵌于其他的XML文档中,而SVG文档中 ...

  2. 【转】Impala导出查询结果到文件

    [转载出处]http://blog.csdn.net/jobschen/article/details/68942574 想用impala-shell 命令行中将查询的结果导出到本地文件,想当然的以为 ...

  3. MyBatis(2)——MyBatis 深入学习

    编写日志输出环境配置文件 在开发过程中,最重要的就是在控制台查看程序输出的日志信息,在这里我们选择使用 log4j 工具来输出: 准备工作: 将[MyBatis]文件夹下[lib]中的 log4j 开 ...

  4. [LeetCode] Cherry Pickup 捡樱桃

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  5. [LeetCode] Replace Words 替换单词

    In English, we have a concept called root, which can be followed by some other words to form another ...

  6. [LeetCode] Find Duplicate File in System 在系统中寻找重复文件

    Given a list of directory info including directory path, and all the files with contents in this dir ...

  7. [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  8. “百度杯”CTF比赛 九月场_再见CMS(齐博cms)

    题目在i春秋ctf大本营 又是一道cms的题,打开御剑一通乱扫,发现后台登录地址,访问一看妥妥的齐博cms 记得以前很久以前利用一个注入通用漏洞,这里我贴上链接,里面有原理与利用方法详细说明: 齐博c ...

  9. 线性规划与网络流24题●09方格取数问题&13星际转移问题

    ●(做codevs1908时,发现测试数据也涵盖了1907,想要一并做了,但因为“技术”不佳,搞了一上午) ●09方格取数问题(codevs1907  方格取数3) 想了半天,也没成功建好图: 无奈下 ...

  10. hdu 5135(2014广州—状态dp)

    t题意:给你n条边,构造任意个三角形,一个三角形恰好只用3条边,每条边只能一次,求面积最大值 思路: 最开始想的是先排序从大到小取,但感觉并不怎么靠谱. 最多12条边,所以可以求出所有可能的三角形面积 ...