TextView点击后背景颜色、文字颜色改变(转)
转自:http://blog.csdn.net/u013278940/article/details/51152655
TextView本没有点击效果,故为实现点击后文字颜色改变和背景改变,需要写selector进行点击时颜色的替换。效果图如下:
未点击时:字颜色为黑色,背景为系统默认颜色。点击时:字体颜色为绿色,背景色为粉色。如下图所示:
布局文件:
res/layout/activity_main.xml
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="60dp"
- android:paddingLeft="25dp"
- android:gravity="center_vertical"
- android:text="@string/hello_world"
- android:textSize="25dp"
- android:textColor="@color/textcolor_selector"
- android:background="@drawable/background_selector"
- android:clickable="true"
- android:focusable="true"/>
文字颜色:
res/color/textcolor_selector.xml
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_focused="true" android:color="@color/green"></item>
- <item android:state_checked="true" android:color="@color/green"></item>
- <item android:state_pressed="true" android:color="@color/green"></item>
- <item android:color="@color/black"/>
- </selector>
背景颜色
res/drawable/background_selector.xml
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android" >
- <item android:state_focused="true" android:drawable="@color/pink"></item>
- <item android:state_checked="true" android:drawable="@color/pink"></item>
- <item android:state_pressed="true" android:drawable="@color/pink"></item>
- </selector>
涉及颜色值:
res/values/color.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <color name="white">#ffffffff</color>
- <color name="black">#ff000000</color>
- <color name="pink">#ffffcbd7</color>
- <color name="green">#ffbae4b6</color>
- </resources>
注意事项:
textview控件默认没有点击和焦点的,所以需要在布局文件中把clickable和focusable的值手动添加为true。
Demo下载:
网址:https://github.com/duhuan2015/TextViewTest 点击下载
TextView点击后背景颜色、文字颜色改变(转)的更多相关文章
- Android中通过xml改变背景及文字颜色
原创文章,转载请注明出处,谢谢! 本篇主要介绍Android开发中,通过XML资源文件来设置控件在不同状态下的背景及文字颜色.关于xml改变背景及文字颜色的原理,大家可以去看一下郭霖大神的源码分析文章 ...
- 设置LISTControl控件某一行的背景和文字颜色
定义宏 用listcontrol的SetItemData设置某一行的属性,通过自定义属性标识实现. 自定义某行内容颜色属性: #define COLOR_DEFAULT 0 //默认颜色 #defin ...
- button改变背景与文字颜色
1.定义/zhsh/res/color/txt_guide_selector.xml <?xml version="1.0" encoding="utf-8&quo ...
- (转)TextView 设置背景和文本颜色的问题
在做一个项目,突然遇到如下问题 比如:在color.xml中定义了几个颜色 <color name="white">#FFFFFF</color> < ...
- SecureCRT的背景和文字颜色的修改
options->;session options->;emulation->;terminal选择linux(相应的服务器系统)ansi color 打上钩options-> ...
- android radiogroup样式(设置切换背景与文字颜色)
main.xml <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_cont ...
- VC改变CListCtrl 表格中文字颜色,和背景颜色。
(1)首先需要自定义一个类,派生自CListCtrl.如下图: (2)然后在派生类的头文件中声明一个成员函数,如下图: (3)在源文件中实现该成员方法,如图: (4)在源文件中做消息映射,如图: 这时 ...
- EditText 详细信息(监听事件时,输入改变、透明背景、提示改变文字颜色、密文输入)
1.对EditText输入监视.给EditText 捆绑 addTextChangedListener 监控事件 能够. 2.EditText输入内容.密文显示: android:password=& ...
- android TextView 设置部分文字背景色和文字颜色
通过SpannableStringBuilder来实现,它就像html里边的元素改变指定文字的文字颜色或背景色 public class MainActivity extends Activity { ...
随机推荐
- spring cloud学习笔记五 网关服务zuul
网关服务是指,客户端发送的请求不用直接访问特定的微服务接口,而且是经过网关服务的接口进行交互,网关服务再去到特定的微服务中进行调用. 网关服务的路由功能和Nginx的反向代理一样,所有的服务都先会 ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- INSTR代替NOT LIKE
instr(title,'手册')>0 相当于 title like '%手册%' instr(title,'手册')=1 相当于 title like '手册%' instr(titl ...
- SpringBoot+Shiro学习(七):Filter过滤器管理
SpringBoot+Shiro学习(七):Filter过滤器管理 Hiwayz 关注 0.5 2018.09.06 19:09* 字数 1070 阅读 5922评论 1喜欢 20 先从我们写的一个 ...
- 51nod 1836:战忽局的手段(期望)
题目链接 公式比较好推 精度好难搞啊@_@ 下面记笔记@_@ ****在CodeBlocks中,输出double型变量要使用%f (参见http://bbs.csdn.net/topics/39193 ...
- APP前置代码
APP自动化前置代码: #导入包from appium import webdriverimport timedesired_caps = {}desired_caps['platformName'] ...
- 【LeetCode 42】接雨水
题目链接 [题解] 考虑每个位置它最后能接多少单位的水. 显然就是这个min(位置左边最高的位置,位置右边最高的位置)-当前这个位置的高度. 这就是这个位置最后水上涨的高度. 两个边界注意是不会储水的 ...
- Activator.CreateInstance with parameters
https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance?view=netframework-4.8#Sy ...
- js中slice、splice、substr、split方法
1.slice 可用于数组与字符串,返回一个新的数组,原数组不改变,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素. 在string中 slice(start,e ...
- bootstrap基础模板页面,详细注释
<!--html5 骨架--> <!DOCTYPE html> <!--语言是中文简体--> <html lang="zh-cn"&g ...