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 { ...
随机推荐
- Altium Designer 19 单层显示
PCB视图下,按快捷键L 弹出对话框 选择 view options 选项卡 Single Layer Mode 点击为on 快捷键为Shift + s
- vue代理配置之二--dev方式启动的index.js配置
'use strict' // Template version: 1.3.1 // see http://vuejs-templates.github.io/webpack for document ...
- C++ KMP文本匹配
代码如下: 环境为VC #include <iostream> #include <algorithm> #include <string> #include &l ...
- 爬虫技术:cookies池的维护
一:为什么要维护cookie 1.登录才能爬取内容 2.爬取频繁会被封号. 3.需要维护多个账号的cookie,实现大规模抓取 二:cookies的要求 1.自动登录更新 2.定期筛选验证 3.提供外 ...
- Centos7.4 离线安装httpd(解决rpm依赖)
1.直接下载httpd的rpm安装包,安装失败需要先解决依赖. [root@node06 ~]# rpm -ivh httpd--.el7.centos.x86_64.rpm warning: htt ...
- Jmeter下载文件和保存文件
Jmeter下载文件: 任意在网上搜索一张图片,地址为https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&a ...
- JQUERY的$(function(){})和window.onload=function(){}的区别【转】
在Jquery里面,我们知道入口函数有两种写法:$(function(){}) 和$(document).ready(function(){}) 作用类似于传统JavaScript中的window.o ...
- 用 Flask 来写个轻博客 (23) — 应用 OAuth 来实现 Facebook 第三方登录
目录 目录 前文列表 扩展阅读 第三方登录流程 OAuth 应用 OAuth 实现 Facebook 第三方登录 实现效果 前文列表 用 Flask 来写个轻博客 (1) - 创建项目 用 Flask ...
- delphi在64位系统下写注册表注意事项
HKEY_LOCAL_MACHINE写这个主键下的项,在64位系统下可能会重定向,所以构造时要加KEY_WOW64_64KEY reg := TRegistry.Create(KEY_WRITE or ...
- vue搭建项目步骤(二)
上篇是搭建Vue项目的基本,接下来是继续对做项目的记录.顺序并不一定. 五.对页面入口文件的修改: 众所周知,main.js 程序入口文件,加载各种公共组件,App.Vue为 页面入口文件.但是有时候 ...