自定义支持多行显示的RadioGroup
自定义支持多行显示的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的更多相关文章
- 简单的自绘CListBox(多行显示)(覆盖DrawItem函数,然后用CDC绘制)
之前写过一个自绘的CListBox类,详细请参考http://blog.csdn.net/VisualEleven/archive/2010/10/12/5935430.aspx现在修改这之前的代码, ...
- Android的有关EditText的能多行显示但无法禁止自动换行的Bug!
需求: 使 EditText或TextView 支持 多行显示,但是不自动换行,即能水平滚动较长的内容. Bug: 想当然的,在XML定义中设置如下,应该就可以了. android:scrollHor ...
- HTML5 元素文字超出部分显示省略号(支持多行),兼容几乎所有常用浏览器
1,公共样式,在公共的 CSS 文件中加入以下内容 /* 超出部分显示省略号,支持多行 */ .text-ells:before { content: ''; float: left; width: ...
- 写了一个迷你toast提示插件,支持自定义提示文字和显示时间
写了一个迷你toast提示插件,支持自定义提示文字和显示时间,不想用其他第三方的ui插件,又想要toast等小效果来完善交互的同学可以试试, 代码中还贡献了一段css能力检测js工具函数,做项目的时候 ...
- MYSQL 命令行显示乱码 解决方案
中文乱码是因为编码集不支持,所以要改变编码 先查看下设置的编码 使用如下命令 show variables like 'character%'; 在 mysql.conf (Ubuntu mysql5 ...
- Android中的TextView实现多行显示省略号
今天遇到一个问题就是TextView显示内容的时候,多行显示的时候,显示省略号的问题,刚开始没有找到一个好的办法,只找到一个自定义TextView组件的方法,然而今天在贴吧中找到一个更好,更简便的方法 ...
- Android 自定义支持快速搜索筛选的选择控件(一)
Android 自定义支持快速搜索筛选的选择控件 项目中遇到选择控件选项过多,需要快速查找匹配的情况. 做了简单的Demo,效果图如下: 源码地址:https://github.com/whieenz ...
- 【WPF】ListBox GridViewColumn Header 文字换行、文字多行显示
ListBox GridViewColumn Header 文字换行.文字多行显示,在Header中需要换行的地方写 <GridViewColumn Header="空间另存 为总量& ...
- jquery.ellipsis根据宽度(不是字数)进行内容截断,支持多行内容
jquery.ellipsis 自动计算内容宽度(不是字数)截断,并加上省略号,内容不受中英文或符号限制. 如果根据字数来计算的话,因为不同字符的宽度并不相同,比如l和W,特别是中英文,最终内容宽度会 ...
随机推荐
- CLR-基元类型以及溢出检查
=========(CLR via C#阅读笔记)======== 基元类型(primitive type): 基元类型也不做过多的解释,举个例子即可清晰的辨别 在java里曾使用过Sting s=& ...
- Java之GC
Java之GC GC:GC 是JVM的垃圾回收器.与C/C++不同,java程序员无需考虑太多内存分配的位置,更不用考虑内存释放的机制,java对象内存的申请和释放都有JVM托管.JVM的内存释放机制 ...
- [BZOJ 5071]小A的数字
Description 小A成为了一个数学家,他有一串数字A1,A2...An 每次可以进行如下操作,选择一个数字i(1<i<=n),将(Ai-1,Ai,Ai+1) 变为(Ai-1 + A ...
- SRM340 VegetableGarden
Description 你的蔬菜园形成了一个矩形网格.你决定检查一些小块土地.从左上角开始,你将走过菜园,回到起点.现在你想要检查一下菜园内的田地,于是你决定从左上角出发,在菜园里走一圈回到原处.最后 ...
- ●CodeForces 518D Ilya and Escalator
题链: http://codeforces.com/problemset/problem/518/D题解: 期望dp. 定义dp[t][i]表示在第t秒开始之前,已经有了i个人在电梯上,之后期望能有多 ...
- 51 nod 1495 中国好区间
1495 中国好区间 基准时间限制:0.7 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 阿尔法在玩一个游戏,阿尔法给出了一个长度为n的序列,他认为,一段好的区间,它的长度是& ...
- hdu 5314 动态树
Happy King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tot ...
- 51nod 1035:最长的循环节
1035 最长的循环节 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求< ...
- hdu 5478 (数论)
⋅n+b1⋅n−k2+1 = 0 (mod C)(n = 1, 2, 3, ...) (1<=a, b <C) 1. 当n = 1时, a^(k1+b1) + b = 0 ( mod ...
- hihocoder 1075 : 开锁魔法III
描述 一日,崔克茜来到小马镇表演魔法. 其中有一个节目是开锁咒:舞台上有 n 个盒子,每个盒子中有一把钥匙,对于每个盒子而言有且仅有一把钥匙能打开它.初始时,崔克茜将会随机地选择 k 个盒子用魔法将它 ...