转 Android RadioButton设置选中时文字和背景颜色同时改变
主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿。
搜了一下,效果和这个文章一致。转了。
原文地址:http://blog.csdn.net/liuwan1992/article/details/52688408
在使用 RadioButton 时,有时我们会想要达到选中时文字颜色和背景颜色同时改变的效果,这里还需要多进行几步操作。
首先,在布局文件中新建一组 RadioButton :
- <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" 这里设置了背景选择器,代码如下:
- <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 ,具体代码如下:
- <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/ 目录下面。代码如下:
- <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 :
- 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 ,然后进行一些你想要的操作。
转 Android RadioButton设置选中时文字和背景颜色同时改变的更多相关文章
- Android RadioButton设置选中时文字和背景颜色同时改变
主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...
- android中设置ListView的选中的Item的背景颜色
ListView中没有默认的选择颜色,只有选择Item后的焦点颜色,鼠标点击时Item有颜色,放开鼠标后颜色也就没有了,要实现放开鼠标后选择项的背景还是有颜色的. 1.配置main.xml <? ...
- css3应用之自定义选中文字的背景颜色
在看很多的博客主题时候发现大多数都对选中文字的背景颜色做了相应的处理.其实这样是很符合用户体验的.因为有很多的人会用鼠标选择着一行一行的阅读.其中就包括本人... 浏览器中默认的选中的文字颜色为白色, ...
- Echarts 设置地图上文字大小及颜色
Echarts 设置地图上文字大小及颜色,效果如下: 上代码:关键代码用红色 series: [ { //name: '香港18区人口密度', type: 'map', mapType: 'jiang ...
- [Xcode 实际操作]五、使用表格-(5)设置UITableView的单元格背景颜色
目录:[Swift]Xcode实际操作 本文将演示单元格背景颜色的设置 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先添加两个协 ...
- pycharm设置开发模板/字体大小/背景颜色(3)
一.pycharm设置字体大小/风格 选择 File –> setting –> Editor –> Font ,可以看到如上界面,可以根据自己的喜好随意调整字体大小,字体风格,文字 ...
- html根据下拉框选中的值修改背景颜色
错误的写法 <!doctype html><html><head><meta charset="utf-8"><title&g ...
- 改变listview中item选中时文字的颜色
摘要 当listview的某个item选中时,默认有个选中的高亮显示,如果你要自定义选中时的高亮显示效果,可以在listview中设置属性 android:listSelector="@dr ...
- Windows 10设置桌面图标间距、窗口的背景颜色、选中文字的背景颜色
Windows 10取消了“高级外观设置”(或者叫“窗口颜色和外观”设置),想调整一些参数只能进注册表了. 修改后可能需要注销或重启才能生效. 按Win+R,然后输入regedit进入注册表编辑器. ...
随机推荐
- jquery实现多级下拉菜单
支持多种浏览器,体验效果:http://keleyi.com/keleyi/phtml/jqmenu/4.htm 多级菜单,理论上支持无限多的层级,文件结构非常简单的,以下是完整代码: <!DO ...
- ["1", "2", "3"].map(parseInt)?
["1", "2", "3"].map(parseInt)得到什么? 答案是:[1, NaN, NaN]. 原因:parseInt接收的是两 ...
- jQuery实用小技巧--输入框文字获取和失去焦点
<input id="txt" class="text1" type="text" /> <script src=& ...
- iOS HTML 字符串中的图片 自适应大小
本文原文地址:http://www.cnblogs.com/qianLL/p/6095988.html 有时候 我们接收数据的时候 后台给的数据室一串HTML 的字符串 但是 我们要显示出来 这 ...
- 【Swift】iOS 9 Core Spotlight
前言 感觉 Spotlight 这个功能还是蛮有用的,能提升用户活跃,增加应用内容曝光几率. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: ...
- Fragment配合RadioGroup实现点击切换布局
这里用了 compile 'com.jakewharton:butterknife:7.0.1' compile 'org.greenrobot:eventbus:3.0.0' MainActivit ...
- 学习Swift的点点滴滴
1.类型标注 之前不知道为啥别人写的Swift语言的时候,定义常量或者变量的格式是 常量: let 常量名: 常量类型 = 常量值 或者 变量: var 变量名: 变量类型 = 初始值 原来书上有记 ...
- AFN3.0封装
总结了一下AFN3.0封装,也借鉴了其他人总结的,整理如下,希望多交流,互相进步 // // XJYSessionManager.h// // Created by XJY on 16/10/17. ...
- 问题解决——MFC error RC2170: bitmap file res\XXXXXXX.png is not in 3.00 format
=================================版权声明================================= 版权声明:原创文章 谢绝转载 请通过右侧公告中的“联系邮 ...
- 实战搭建SVN代码版本服务器
前言:公司要求搭建一台SVN代码版本管理服务器,用于管理所有代码资产: 项目架构图 1.环境安装 [root@host_centos ~]#yum –y install subversion mod_ ...