主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿。

搜了一下,效果和这个文章一致。转了。

原文地址:http://blog.csdn.net/liuwan1992/article/details/52688408

在使用 RadioButton 时,有时我们会想要达到选中时文字颜色和背景颜色同时改变的效果,这里还需要多进行几步操作。

首先,在布局文件中新建一组 RadioButton :

  1. <RadioGroup
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:gravity="center"
  5. android:orientation="horizontal">
  6. <RadioButton
  7. android:id="@+id/btn1"
  8. android:layout_width="0dp"
  9. android:layout_height="35dp"
  10. android:layout_weight="1"
  11. android:background="@drawable/radiobutton_background"
  12. android:button="@null"
  13. android:gravity="center"
  14. android:text="P0501"
  15. android:textColor="@color/radiobutton_textcolor"
  16. android:textSize="14sp" />
  17. <RadioButton
  18. android:id="@+id/btn2"
  19. android:layout_width="0dp"
  20. android:layout_height="35dp"
  21. android:layout_marginStart="10dp"
  22. android:layout_weight="1"
  23. android:background="@drawable/radiobutton_background"
  24. android:button="@null"
  25. android:gravity="center"
  26. android:text="P0502"
  27. android:textColor="@color/radiobutton_textcolor"
  28. android:textSize="14sp" />
  29. <RadioButton
  30. android:id="@+id/btn3"
  31. android:layout_width="0dp"
  32. android:layout_height="35dp"
  33. android:layout_marginStart="10dp"
  34. android:layout_weight="1"
  35. android:background="@drawable/radiobutton_background"
  36. android:button="@null"
  37. android:gravity="center"
  38. android:text="P0503"
  39. android:textColor="@color/radiobutton_textcolor"
  40. android:textSize="14sp" />
  41. </RadioGroup>

这里面有三个属性要做一下说明:

1、Android:button="@null" 这样设置可以不显示我们通常所见的 RadioButton 中的圆形选中按钮.

2、android:background="@drawable/radiobutton_background" 这里设置了背景选择器,代码如下:

  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item android:drawable="@drawable/radiobutton_background_unchecked"
  3. android:state_checked="false" />
  4. <item android:drawable="@drawable/radiobutton_background_checked"
  5. android:state_checked="true" />
  6. </selector>

这里面的选中样式又指向一个 Drawable 资源文件 radiobutton_background_checked.xml ,具体代码如下:

  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:shape="rectangle">
  3. <!-- 填充 -->
  4. <solid android:color="@color/color14" />
  5. <!-- 圆角 -->
  6. <corners android:radius="5dp" />
  7. </shape>

以上这些资源文件都放在 res/drawable/ 目录下。

3、android:textColor="@color/radiobutton_textcolor" 这里设置了字体颜色选择器,需要稍作说明的是:需要在 res 目录下新建一个

文件夹取名为 color ,将字体颜色选择器 radiobutton_textcolor.xml 文件存放在 res/color/ 目录下面。代码如下:

  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item android:color="@color/color2"
  3. android:state_checked="false" />
  4. <item android:color="@color/color1"
  5. android:state_checked="true" />
  6. </selector>

经过以上步骤后,我们来看一下效果图:

     

最后提一下怎么通过 RadioGroup 获取 RadioButton :

  1. RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
  2. radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  3. @Override
  4. public void onCheckedChanged(RadioGroup group, int checkedId) {
  5. RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
  6. String result = radioButton.getText().toString();
  7. }
  8. });

这样就可以获取到当前 RadioGroup 中选中的 RadioButton ,然后进行一些你想要的操作。

转 Android RadioButton设置选中时文字和背景颜色同时改变的更多相关文章

  1. Android RadioButton设置选中时文字和背景颜色同时改变

    主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...

  2. android中设置ListView的选中的Item的背景颜色

    ListView中没有默认的选择颜色,只有选择Item后的焦点颜色,鼠标点击时Item有颜色,放开鼠标后颜色也就没有了,要实现放开鼠标后选择项的背景还是有颜色的. 1.配置main.xml <? ...

  3. css3应用之自定义选中文字的背景颜色

    在看很多的博客主题时候发现大多数都对选中文字的背景颜色做了相应的处理.其实这样是很符合用户体验的.因为有很多的人会用鼠标选择着一行一行的阅读.其中就包括本人... 浏览器中默认的选中的文字颜色为白色, ...

  4. Echarts 设置地图上文字大小及颜色

    Echarts 设置地图上文字大小及颜色,效果如下: 上代码:关键代码用红色 series: [ { //name: '香港18区人口密度', type: 'map', mapType: 'jiang ...

  5. [Xcode 实际操作]五、使用表格-(5)设置UITableView的单元格背景颜色

    目录:[Swift]Xcode实际操作 本文将演示单元格背景颜色的设置 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先添加两个协 ...

  6. pycharm设置开发模板/字体大小/背景颜色(3)

    一.pycharm设置字体大小/风格 选择 File –> setting –> Editor –> Font ,可以看到如上界面,可以根据自己的喜好随意调整字体大小,字体风格,文字 ...

  7. html根据下拉框选中的值修改背景颜色

    错误的写法 <!doctype html><html><head><meta charset="utf-8"><title&g ...

  8. 改变listview中item选中时文字的颜色

    摘要 当listview的某个item选中时,默认有个选中的高亮显示,如果你要自定义选中时的高亮显示效果,可以在listview中设置属性 android:listSelector="@dr ...

  9. Windows 10设置桌面图标间距、窗口的背景颜色、选中文字的背景颜色

    Windows 10取消了“高级外观设置”(或者叫“窗口颜色和外观”设置),想调整一些参数只能进注册表了. 修改后可能需要注销或重启才能生效. 按Win+R,然后输入regedit进入注册表编辑器. ...

随机推荐

  1. ECharts外部调用保存为图片操作及工作流接线mouseenter和mouseleave由于鼠标移动速度过快导致问题解决办法

    记录两个项目开发中遇到的问题,一个是ECharts外部调用保存为图片操作,一个是workflow工作流连接曲线onmouseenter和onmouseleave事件由于鼠标移动过快触发问题. 一.外部 ...

  2. tomcat accesslog日志扩展

    由于工作需要,最近对tomcat的日志进行了一些研究,发现其日志大致可以分为两类,一类是运行日志,即平常我们所说的catalina.out日志,由tomcat内部代码调用logger打印出来的:另一类 ...

  3. SharePoint 2013 对二进制大型对象(BLOB)进行爬网

    本文是参考MSDN文档做的示例,SharePoint 2013搜索二进制对象(BLOB),通过外部内容类型的方式将外部数据与SharePoint相关联,修改BCD模型,使SharePoint能够爬网外 ...

  4. Alvin

    Alvin Zhao 東京都 港区虎ノ門2-10-4 ホテルオークラ東京 M 663 電話番号: 0335820111

  5. iOS之ShareSDK实现分享、第三方登录等功能

    (1)官方下载ShareSDK iOS 2.8.8,地址:http://sharesdk.cn/ (2)根据实际情况,引入相关的库,参考官方文档. (3)在项目的AppDelegate中一般情况下有三 ...

  6. 二进制配置文件为什么比json等配置文件效率高

    二进制配置文件为什么比json等配置文件高效 项目中用spine做动画,spine可以导出json和二进制的动画配置文件,蛋疼的是spine官方竟然没有提供c的二进制配置解析代码,更没有提供它二进制文 ...

  7. Android 面试题汇总

    面试题基础储备 1.Activity相关 a.Activity的特点 1.可见  2.可交互 他之所以可交互,是因为他同时实现了Window.Callback和KeyEvent.Callback, 可 ...

  8. MySQL学习笔记之视图

    视图是对磁盘上保存的表数据的抽象,即抽取一个表或多个表的部分行或列的数据,展示给使用者. 首先列举下MySQL中最简单的对视图操作的语法: 1.创建视图: create view 视图名 as sel ...

  9. linux top 源码分析

    /* * Copyright (c) 2008, The Android Open Source Project * All rights reserved. * * Redistribution a ...

  10. mybatis配置-返回date类型丢失时间

    此博客仅作于平时开发所遇到的问题记录,不做他用,描述可能不好,自己看懂即可~~ resultMap配置返回时间类型时,发现数据库时间是精确到秒的,但是返回给javabean之后丢失时分秒的信息,只有日 ...