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 按 ...
随机推荐
- 为什么我们不用JIRA
很多人问我,缺陷管理工具,为什么不用jira?而去自己造轮子开发一款bug记录系统 缄默如我,原因众多.如果只是3-5分钟就能讲的请的时候,我会先列出什么糟点呢? 1. 收费,一个人一个月的费用差不多 ...
- 【0808 | Day 11】文件的高级应用/修改以及函数的定义/使用/参数
文件的高级应用 一.三种模式 'r+'模式 with open('test.py','r',encoding = 'utf8') as fr: print(fr.writable()) fr.writ ...
- JavaWeb配置详解(结合框架SpringMVC)
详解 先说一说常识性的东西,我们的JavaWeb程序运行一开始走的是web.xml文件,这是我们的核心文件,可以说没有web.xml文件我们就无法运行项目,这个文件长什么样子,读者自己新建一个web项 ...
- Shell脚本书写规范
在日常的运维工作中,Shell脚本肯定是必不可少的工作内容.为方便问题排查.脚本执行历史问题追踪.方便大家共同维护,从网上搜罗结合以往的经验教训拟定以下Bash脚本书写规范.欢迎各位同学指正或补充. ...
- Linux:oracle11.2.0dbca静默建库
1.关闭防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall ...
- asp.net core 从单机到集群
asp.net core 从单机到集群 Intro 这篇文章主要以我的活动室预约的项目作为示例,看一下一个 asp.net core 应用从单机应用到分布式应用需要做什么. 示例项目 活动室预约提供了 ...
- Spring Boot之Profile--快速搞定多环境使用与切换
Spring Profile是Spring3引入的概念,主要用在项目多环境运行的情况下,通过激活方式实现多环境切换,省去多环境切换时配置参数和文件的修改,并且Spring profile提供了多种激活 ...
- Stream和方法引用
1.Stream流 1.for循环带来的弊端 在jdk8中,lambda专注于做什么,而不是怎么做 for循环的语法就是怎么做 for循环的循环体才是做什么 遍历是指每一个元素逐一进行处理,而并不是从 ...
- Tomcat源码分析 (六)----- Tomcat 启动过程(一)
说到Tomcat的启动,我们都知道,我们每次需要运行tomcat/bin/startup.sh这个脚本,而这个脚本的内容到底是什么呢?我们来看看. 启动脚本 startup.sh 脚本 #!/bin/ ...
- 【KakaJSON手册】05_JSON转Model_05_动态模型
在上一篇文章中提到:有时候服务器返回的某个字段的内容类型可能是不确定的 当时给出的解决方案是实现kk_modelValue或者kk_didConvertToModel方法,根据实际需求自定义JSON的 ...