最近项目用到了多行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的使用的更多相关文章

  1. Android中使用RadioButton代替ImageButton

    画外音————好久没上来发文章了,这几个月一直忙着一些跟编程不沾边的事,拖了好久,现在还在持续中,顺利的话7月份应该能解放了..今天偶尔上来写一段番外篇性质的心得发现. 之前搞的Android项目,作 ...

  2. Android命令行播放MP3音乐

    /*************************************************************************** * Android命令行播放MP3音乐 * 说 ...

  3. android串行化getSerializable、getSerializableExtra

    android串行化getSerializable.getSerializableExtra 传参 总结 案例1 不用 Bundle 封装数据 提交activity lst.setOnItemClic ...

  4. 《Android第一行代码》笔记

    学习Android开发差点儿相同有两年时间了.期间也做了大大小小的一些项目.近来抽出闲暇想把Android基础强化一下,之前在网上看到了郭霖郭大神的几篇博客.从中受益不少.于是花了近一周时间看完了郭神 ...

  5. android 命令行安装apk

    有两种方式可以在android模拟器或真机上使用命令行安装apk 一种是使用adb install命令,网上通常是这种方式 另一种是通过android提供的命令,pm install. 需要先进入an ...

  6. Android 命令行模拟按键

    /***************************************************************************** * Android 命令行模拟按键 * 说 ...

  7. Android RadioGroup的RadioButton 选择改变字体颜色和背景颜色

    RadioGroup <RadioGroup android:id="@+id/client_charge_radiogroup" android:layout_width= ...

  8. Android系列之Android 命令行手动编译打包详解

    Android 命令行手动编译打包过程图 [详细步骤]: 1使用aapt生成R.java类文件:  例:  E:\androidDev\android-sdk-windows2.2\tools> ...

  9. Android Studio 之 RadioButton

    •任务 如何通过 RadioButton 实现如图所示的界面? •基本用法 RadioButton 单选按钮,就是只能够选中一个,所以我们需要把 RadioButton 放到 RadioGroup 按 ...

随机推荐

  1. Z算法

    Z算法 Z算法是一种用于字符串匹配的算法.此算法的核心在于\(z\)数组以及它的求法. (以下约定字符串下标从\(1\)开始) \(\bm z\)数组和Z-box 定义\(z\)数组:\(z_{a,i ...

  2. 100天搞定机器学习|day38 反向传播算法推导

    往期回顾 100天搞定机器学习|(Day1-36) 100天搞定机器学习|Day37无公式理解反向传播算法之精髓 上集我们学习了反向传播算法的原理,今天我们深入讲解其中的微积分理论,展示在机器学习中, ...

  3. 前端利器躬行记(1)——npm

    npm(Node Package Manager)是Node.js的包管理工具,相当于一个在线仓库.它提供了一个公共的平台,将分散在世界各地的包集中起来,能轻松的安装.分享和管理相关的包,不用再为搜索 ...

  4. Tomcat 方式部署 Solo 博客系统总结

      此篇为Tomcat部署方式,另有Docker部署方式,请参考文章<Docker 方式部署 Solo 博客系统总结> 一.环境和文件准备 服务器:购买的阿里云服务器,系统为Linux(C ...

  5. Go语言-基本的http请求操作

    Go发起GET请求 基本的GET请求 //基本的GET请求 package main import ( "fmt" "io/ioutil" "net/ ...

  6. Greenplum数据库分享

    1. 场景描述 最近做了次Greenplum数据库的分享,如下图,把第三章的的架构介绍简单提出来,分享下. 2. 解决方案 就按照ppt贴图了,部分内容稍微做了下马赛克. (这页ppt的下半部分,有实 ...

  7. 纯数据结构Java实现(5/11)(Set&Map)

    纯数据结构Java实现(5/11)(Set&Map) Set 和 Map 都是抽象或者高级数据结构,至于底层是采用树还是散列则根据需要而定. 可以细想一下 TreeMap/HashMap, T ...

  8. 如何运用PHP+REDIS解决负载均衡后的session共享问题

    一.为什么要使用Session共享? 稍大一些的网站,通常都会有好几个服务器,每个服务器运行着不同功能的模块,使用不同的二级域名,而一个整体性强的网站,用户系统是统一的,即一套用户名.密码在整个网站的 ...

  9. Nginx总结(三)基于端口的虚拟主机配置

    前面讲了如何配置基于IP的虚拟主机,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天就 ...

  10. jvm系列(四):jvm调优-命令篇

    运用jvm自带的命令可以方便的在生产监控和打印堆栈的日志信息帮忙我们来定位问题!虽然jvm调优成熟的工具已经有很多:jconsole.大名鼎鼎的VisualVM,IBM的Memory Analyzer ...