android 多行 RadioButton的使用
最近项目用到了多行RadioButton,随记录下.
先给出RadioButton的布局
<com.kuibu.jucai.widget.MyRadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <RadioButton
android:id="@+id/rb_50"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="@dimen/space_10"
android:layout_weight="1"
android:background="@drawable/poundage_selector"
android:button="@null"
android:gravity="center"
android:paddingBottom="@dimen/space_10"
android:paddingLeft="@dimen/space_20"
android:paddingRight="@dimen/space_20"
android:paddingTop="@dimen/space_10"
android:text="50¥"
android:textColor="@drawable/fg_order_text_selector" /> <RadioButton
android:id="@+id/rb_100"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="@dimen/space_10"
android:layout_weight="1"
android:background="@drawable/poundage_selector"
android:button="@null"
android:gravity="center"
android:paddingBottom="@dimen/space_10"
android:paddingLeft="@dimen/space_20"
android:paddingRight="@dimen/space_20"
android:paddingTop="@dimen/space_10"
android:text="100¥"
android:textColor="@drawable/fg_order_text_selector" /> <RadioButton
android:id="@+id/rb_150"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="@dimen/space_10"
android:layout_marginRight="@dimen/space_10"
android:layout_weight="1"
android:background="@drawable/poundage_selector"
android:button="@null"
android:gravity="center"
android:paddingBottom="@dimen/space_10"
android:paddingLeft="@dimen/space_30"
android:paddingRight="@dimen/space_30"
android:paddingTop="@dimen/space_10"
android:text="150¥"
android:textColor="@drawable/fg_order_text_selector" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <RadioButton
android:id="@+id/rb_200"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="@dimen/space_10"
android:layout_marginTop="@dimen/space_10"
android:layout_weight="1"
android:background="@drawable/poundage_selector"
android:button="@null"
android:gravity="center"
android:paddingBottom="@dimen/space_10"
android:paddingLeft="@dimen/space_20"
android:paddingRight="@dimen/space_20"
android:paddingTop="@dimen/space_10"
android:text="200¥"
android:textColor="@drawable/fg_order_text_selector" /> <RadioButton
android:id="@+id/rb_500"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="@dimen/space_10"
android:layout_marginTop="@dimen/space_10"
android:layout_toRightOf="@+id/rb_200"
android:layout_weight="1"
android:background="@drawable/poundage_selector"
android:button="@null"
android:gravity="center"
android:paddingBottom="@dimen/space_10"
android:paddingLeft="@dimen/space_20"
android:paddingRight="@dimen/space_20"
android:paddingTop="@dimen/space_10"
android:text="500¥"
android:textColor="@drawable/fg_order_text_selector" /> <RadioButton
android:id="@+id/rb_1000"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="@dimen/space_10"
android:layout_marginRight="@dimen/space_10"
android:layout_marginTop="@dimen/space_10"
android:layout_toRightOf="@+id/rb_500"
android:layout_weight="1"
android:background="@drawable/poundage_selector"
android:button="@null"
android:gravity="center"
android:paddingBottom="@dimen/space_10"
android:paddingLeft="@dimen/space_20"
android:paddingRight="@dimen/space_20"
android:paddingTop="@dimen/space_10"
android:text="1000¥"
android:textColor="@drawable/fg_order_text_selector" />
</LinearLayout>
</com.kuibu.jucai.widget.MyRadioGroup> 这里用到了一个自定义控件,如下
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);
}
}
}
}
}
}
} 网络不好,无法直接插入代码,只能这样拷贝了.
android 多行 RadioButton的使用的更多相关文章
- Android中使用RadioButton代替ImageButton
画外音————好久没上来发文章了,这几个月一直忙着一些跟编程不沾边的事,拖了好久,现在还在持续中,顺利的话7月份应该能解放了..今天偶尔上来写一段番外篇性质的心得发现. 之前搞的Android项目,作 ...
- Android命令行播放MP3音乐
/*************************************************************************** * Android命令行播放MP3音乐 * 说 ...
- android串行化getSerializable、getSerializableExtra
android串行化getSerializable.getSerializableExtra 传参 总结 案例1 不用 Bundle 封装数据 提交activity lst.setOnItemClic ...
- 《Android第一行代码》笔记
学习Android开发差点儿相同有两年时间了.期间也做了大大小小的一些项目.近来抽出闲暇想把Android基础强化一下,之前在网上看到了郭霖郭大神的几篇博客.从中受益不少.于是花了近一周时间看完了郭神 ...
- android 命令行安装apk
有两种方式可以在android模拟器或真机上使用命令行安装apk 一种是使用adb install命令,网上通常是这种方式 另一种是通过android提供的命令,pm install. 需要先进入an ...
- Android 命令行模拟按键
/***************************************************************************** * Android 命令行模拟按键 * 说 ...
- Android RadioGroup的RadioButton 选择改变字体颜色和背景颜色
RadioGroup <RadioGroup android:id="@+id/client_charge_radiogroup" android:layout_width= ...
- Android系列之Android 命令行手动编译打包详解
Android 命令行手动编译打包过程图 [详细步骤]: 1使用aapt生成R.java类文件: 例: E:\androidDev\android-sdk-windows2.2\tools> ...
- Android Studio 之 RadioButton
•任务 如何通过 RadioButton 实现如图所示的界面? •基本用法 RadioButton 单选按钮,就是只能够选中一个,所以我们需要把 RadioButton 放到 RadioGroup 按 ...
随机推荐
- jquery验证大全
jQuery验证及限制 绑定键盘监听事件 $(document).on("keypress", ".txt-valid-len", function (e) { ...
- ES 26 - 通过partial update局部更新索引文档 (partial update增量修改原理)
目录 1 什么是partial update 1.1 全量修改文档的原理 1.2 修改指定field的思路 1.3 partial update的优势 1.4 partial update的使用 2 ...
- SQL获取客户端网卡电脑名称等信息
Select SYSTEM_USER 当前用户名, USER_NAME() 当前所有者,db_Name() 当前数据库,@@SPID 当前进程号,(select top 1 FileName from ...
- Django Mysql数据库-F查询和Q查询
一.F查询和Q查询 F查询: 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢? Django 提供 F() 来做这样的比较.F() 的 ...
- 浅谈UART/12C/TTL的定义与区别与解析
UART/12C/TTL的定义与区别: UART:UART(Universal Asynchronous Receive Transmitter):也就是我们经常所说的串口,基本都用于调试.主机和从机 ...
- Mac查看及清理QQ、微信之前下载的图片、视频或DB等
之前写过一篇清理Mac空间的文章: Mac系统清理.占用空间大.空间不够.查看系统文件大小分布 其实这篇文章不是太全,有些资源还是清理不彻底,正好前段时间需要找微信下载的资源,其实可以算作空间清理的续 ...
- (四十五)c#Winform自定义控件-水波图表
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- c11标准
在编译器vs13及其以上可以使用 编译器对语言的一种优化 1.变量初始化 int a=0,a(10),a{10};定义a的值的三种方式 2.nullptr 相当于c的null 有类型 更加的安全 3. ...
- urllib.request.urlretrieve()用于下载制定url内容到本地
函数:urllib.urlretrieve(url[, filename[, reporthook[, data]]]) 参数说明: url:外部或者本地url ,url中不要含有中文,好像会出错.f ...
- idea 开发中常用的26个快捷键!
------- udea中窗口最大化---->Windows 上: Ctrl+Shift+F12 -------- ctrl+z撤回代码 ------- c ...