RadioButton 的使用
两个RadioButton来回选择,背景和文字相应跟着变换。选择RadioButton1时候,1的背景变成白色,文字变成红色;2的背景变成红色,文字变成白色。选择RadioButton2时候,1的背景变成红色,文字变成白色;2的背景变成白色,文字变成红色。在使用时我在布局文件中的background和textcolor都使用了@drawable/xxxx,xxxx代表颜色选择器。
后来一直出现android.content.res.Resources$NotFoundException: File res/drawable/markettop.xml from drawable resource ID #0x7f020079的错误。后来改成
background的选择器中的color改成drawable的图片,textcolor的没动,还是color的属性,
就是说,textcolor使用第一张图,background使用第二张图,就可以同时改变radioButton的背景和文字颜色了。
另外附一篇博客:http://blog.csdn.net/liuwan1992/article/details/52688408
Android RadioButton设置选中时文字和背景颜色同时改变
在使用 RadioButton 时,有时我们会想要达到选中时文字颜色和背景颜色同时改变的效果,这里还需要多进行几步操作。
首先,在布局文件中新建一组 RadioButton :
[html] view plain copy
- <RadioGroup
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:orientation="horizontal">
- <RadioButton
- android:id="@+id/btn1"
- android:layout_width="0dp"
- android:layout_height="35dp"
- android:layout_weight="1"
- android:background="@drawable/radiobutton_background"
- android:button="@null"
- android:gravity="center"
- android:text="P0501"
- android:textColor="@color/radiobutton_textcolor"
- android:textSize="14sp" />
- <RadioButton
- android:id="@+id/btn2"
- android:layout_width="0dp"
- android:layout_height="35dp"
- android:layout_marginStart="10dp"
- android:layout_weight="1"
- android:background="@drawable/radiobutton_background"
- android:button="@null"
- android:gravity="center"
- android:text="P0502"
- android:textColor="@color/radiobutton_textcolor"
- android:textSize="14sp" />
- <RadioButton
- android:id="@+id/btn3"
- android:layout_width="0dp"
- android:layout_height="35dp"
- android:layout_marginStart="10dp"
- android:layout_weight="1"
- android:background="@drawable/radiobutton_background"
- android:button="@null"
- android:gravity="center"
- android:text="P0503"
- android:textColor="@color/radiobutton_textcolor"
- android:textSize="14sp" />
- </RadioGroup>
这里面有三个属性要做一下说明:
1、Android:button="@null" 这样设置可以不显示我们通常所见的 RadioButton 中的圆形选中按钮.
2、android:background="@drawable/radiobutton_background" 这里设置了背景选择器,代码如下:
[html] view plain copy
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable="@drawable/radiobutton_background_unchecked"
- android:state_checked="false" />
- <item android:drawable="@drawable/radiobutton_background_checked"
- android:state_checked="true" />
- </selector>
这里面的选中样式又指向一个 Drawable 资源文件 radiobutton_background_checked.xml ,具体代码如下:
[html] view plain copy
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
- <!-- 填充 -->
- <solid android:color="@color/color14" />
- <!-- 圆角 -->
- <corners android:radius="5dp" />
- </shape>
以上这些资源文件都放在 res/drawable/ 目录下。
3、android:textColor="@color/radiobutton_textcolor" 这里设置了字体颜色选择器,需要稍作说明的是:需要在 res 目录下新建一个
文件夹取名为 color ,将字体颜色选择器 radiobutton_textcolor.xml 文件存放在 res/color/ 目录下面。代码如下:
[html] view plain copy
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:color="@color/color2"
- android:state_checked="false" />
- <item android:color="@color/color1"
- android:state_checked="true" />
- </selector>
经过以上步骤后,我们来看一下效果图:
最后提一下怎么通过 RadioGroup 获取 RadioButton :
[java] view plain copy
- RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
- radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
- String result = radioButton.getText().toString();
- }
- });
这样就可以获取到当前 RadioGroup 中选中的 RadioButton ,然后进行一些你想要的操作。
RadioButton 的使用相关推荐
- Android RadioButton 修改选择框
效果图 是否被选中打印效果图 RadioButon 是单选框 ,选中之后再次点击无法取消,这样呢我们可以配合RadioGroup 使用,选择其他的RadioButton 这样的话刚才的的那个就取消了 ...
- android radiobutton 分组,Android 第十课——UI RadioButton
常用基础空组件 5 RadioGroup RadioButton 单选按钮时常用组件之一,但是开发过程中虽然很多地方会用到单选按钮,但是却不会用Android系统提供的原始样式,类似我们在写html时 ...
- 【Visual Studio 扩展工具】如何在ComponentOneFlexGrid树中显示RadioButton
概述 在ComponentOne Enterprise .NET控件集中,FlexGrid表格控件是用户使用频率最高的控件之一.它是一个功能强大的数据管理工具,轻盈且灵动,以分层的形式展示数据(数据呈 ...
- android 自定义radiobutton 样式,RadioButton自定义点击时的背景颜色
一.应用场景: 在使用 RadioButton 时,有时我们会需要自定义点击时改变背景颜色的效果. 效果图: RadioButton自定义背景颜色.jpg 二.实现步骤: 1.新建一组 RadioBu ...
- Android UI系列-----CheckBox和RadioButton(1)
主要记录一下CheckBox多选框和RadioGroup.RadioButton单选框的设置以及注册监听器 1.CheckBox 布局文件: <LinearLayout xmlns:androi ...
- 【Python】Radiobutton组件 LabelFrame组件 Entry组件
Radiobutton组件 Radiobutton组件跟Checkbutton组件的用法基本一致,唯一不同的是Radiobutton实现的是"单选"的效果. 要实现这种互斥的效果, ...
- RadioGroup,RadioButton 和CheckBox
为什么80%的码农都做不了架构师?>>> RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox ...
- WPF中RadioButton绑定数据的正确方法
RadioButton一般用于单选的时候,也就是从一组值中选择一个值. 比如性别有"男"和"女"两种取值,而对于一个员工的实例来说,性别的取值要么是男,要么是女 ...
- Android 定制RadioButton样式
三种方法 1.使用XML文件进行定义 res/drawable/radio.xml <?xml version="1.0" encoding="utf-8" ...
- Material Designer的低版本兼容实现(十)—— CheckBox RadioButton
ChekBox的用途我们就不必多说了,算是一个很古老的控件了,何其类似的还有RadioButton,这个东西因为我目前还没写出来,所以用了别人的一个lib,这下面会说到.顺便说一句,如果你的app是在 ...
最新文章
- MySQL------MySQL与SQLServer数据类型的转换
- Apache优化配置——工作模式
- python画二维数组散点图_Python散点图二维数组
- AIProCon在线大会笔记之张钹院士:探索第三代人工智能,需要勇闯无人区的人才!
- [].slice.call(arguments)模拟实现 Array.of
- Django从理论到实战(part16)--指定默认的参数
- python 对象转dict_如何将python dict对象转换为java等效对象?
- 前端学习(1169):实例数组find
- [HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)
- 斯皮尔 皮尔森 肯德尔_一起来学应用统计学(全部)(二)持续更新
- 485通讯线是几芯的_小令老师说门禁| 为什么485门禁必然会取代韦根?
- 回调破前高意味着什么_4连阳后是回调还是突破呢?
- 项目里面遇到的问题和解决方案的记录
- 简单学习Infopath
- DPDK Release 21.02
- 生活在深圳我们需要的不是叹息与感概
- 【转载】详解Android中接口回调、方法回调
- IoT名企:物联网云服务龙头企业软硬实力兼备,机智云喜获高新技术企业认定
- 信息编码 — 进大厂必备底层能力之一(文末有彩蛋)
- Java的四种遍历方式