最近项目用到了多行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. CODING 告诉你如何建立一个 Scrum 团队

    原文地址:https://www.atlassian.com/agile/scrum/roles 翻译君:CODING 敏杰小王子 Scrum 当中有三个角色:PO(product owner),敏捷 ...

  2. C语言tips_1 关于&& || ! 的优先级

    关于&& || ! 三种操作的优先级 测试如下 简要分析 假设&&>|| 则结果为1 假设||>&& 则结果为0 结果为1 得证 & ...

  3. java中的各种锁详细介绍

    转自:https://blog.csdn.net/axiaoboge/article/details/84335452 Java提供了种类丰富的锁,每种锁因其特性的不同,在适当的场景下能够展现出非常高 ...

  4. 循环while和for

    1.循环语句的基本操作 #while循环使用,其中break是用来结束当前循环的 count = 0 while True: print(count) count += 1 if count == 3 ...

  5. 浅谈python中文件和文件夹的相关操作

    文件操作 文件的打开与关闭 打开文件 使用open(文件名,访问方式)函数,可以打开一个已存在的文件,或者创建一个新的文件. 示例如下: f = open('test.txt') # 访问方式可以省略 ...

  6. Sublime Text 3 使用手册

    Ctrl+Shift+P:打开命令面板 Ctrl+P:搜索项目中的文件 Ctrl+G:跳转到第几行 Ctrl+W:关闭当前打开文件 Ctrl+Shift+W:关闭所有打开文件 Ctrl+Shift+V ...

  7. java学习二

    一.类 1.类是模子,确定对象将会拥有的特征(属性)和行为(方法) 2.类的特点: (1).类是对象的类型 (2).具有相同属性和方法的一组对象的集合 3.类是抽象的概念,仅仅是模板,比如说:“手机” ...

  8. Okhttp3源码解析(2)-Request分析

    ### 前言 前面我们讲了 [Okhttp的基本用法](https://www.jianshu.com/p/8e404d9c160f) [Okhttp3源码解析(1)-OkHttpClient分析]( ...

  9. tab栏切换制作

    tab栏切换制作 先上图 要求1:默认状态,第一个选项卡被选中,展示第一个选项卡的内容 策略:第一个选项卡默认有被选中的样式,第一个选项卡对应的display: block,其他的dispaly设为n ...

  10. python所有的内置异常类型汇总

    内置异常基类 在 Python 中,所有异常必须为一个派生自 BaseException 的类的实例. 通过子类化创建的两个不相关异常类永远是不等效的,既使它们具有相同的名称. 下列异常主要被用作其他 ...