自定义支持多行显示的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. PHP 抓取网页内容的几个函数

    <?php //获取所有内容url保存到文件 function get_index($save_file, $prefix="index_"){ $count = 68; $ ...

  2. 关于if后面直接加上参数名,不加条件的用法

    <template> <section> <p v-if="aa">{{aa}}</p> <p v-if="bb&q ...

  3. svg param.js的大bug

    在svg文件里定义控件,带参数,然后引用. 如果是 text 且没有为其它添加默认值,那么会报错. 即, <svg width="200" height="200& ...

  4. 实验吧_程序逻辑问题(代码审计)&上传绕过

    一开始我先随便输入了几个账号名字进行测试,发现当输入的账号名为admin时会发生报错 经过测试果然是一个注入点 当拿到admin密码后发现根本没用,没办法另寻他路 审查元素时发现提示index.txt ...

  5. JMeter如何使用用户定义的变量

    jmeter中经常使用用户自定义变量,以下是如何在实战中使用自定义变量. 先用Badboy录制登陆脚本,具体的Badboy录制脚本方法自行百度.录制完毕后修改脚本,删除一些没有用的脚本.这里我只保留了 ...

  6. clique

    [题目描述]数轴上有 n 个点,第 i 个点的坐标为 xi,权值为 wi.两个点 i,j 之间存在一条边当且仅当 abs(xi-xj)>=wi+wj.你需要求出这张图的最大团的点数.(团就是两两 ...

  7. 2015 多校联赛 ——HDU5375(dp)

    Sample Input 2 00?0 1 2 4 8 ???? 1 2 4 8   Sample Output Case #1: 12 Case #2: 15 ?部分可以是0  or  1,将二进制 ...

  8. bzoj3786星系探索 splay

    3786: 星系探索 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1314  Solved: 425[Submit][Status][Discuss ...

  9. [bzoj4161]Shlw loves matrix I

    来自FallDream的博客,未经允许,请勿转载,谢谢. 给定数列 {hn}前k项,其后每一项满足 hn = a1*h(n-1) + a2*h(n-2) + ... + ak*h(n-k) 其中 a1 ...

  10. NFC Spy:基于Android 4.4及以上手机的非接智能卡跟踪仪

    NFC Spy 用来查看读卡器和智能卡之间的指令.数据的交互传输过程,以便 NFC/HCE 开发者分析研究底层通讯协议,定位错误指令. 本程序要使用两部带有 NFC 硬件的 Android 手机,并且 ...