转自:http://blog.csdn.net/u013278940/article/details/51152655

TextView本没有点击效果,故为实现点击后文字颜色改变和背景改变,需要写selector进行点击时颜色的替换。效果图如下:

未点击时:字颜色为黑色,背景为系统默认颜色。点击时:字体颜色为绿色,背景色为粉色。如下图所示:

布局文件:

res/layout/activity_main.xml

  1. <TextView
  2. android:layout_width="fill_parent"
  3. android:layout_height="60dp"
  4. android:paddingLeft="25dp"
  5. android:gravity="center_vertical"
  6. android:text="@string/hello_world"
  7. android:textSize="25dp"
  8. android:textColor="@color/textcolor_selector"
  9. android:background="@drawable/background_selector"
  10. android:clickable="true"
  11. android:focusable="true"/>

文字颜色:

res/color/textcolor_selector.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_focused="true" android:color="@color/green"></item>
  4. <item android:state_checked="true" android:color="@color/green"></item>
  5. <item android:state_pressed="true" android:color="@color/green"></item>
  6. <item android:color="@color/black"/>
  7. </selector>

背景颜色
res/drawable/background_selector.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item android:state_focused="true" android:drawable="@color/pink"></item>
  4. <item android:state_checked="true" android:drawable="@color/pink"></item>
  5. <item android:state_pressed="true" android:drawable="@color/pink"></item>
  6. </selector>

涉及颜色值:

res/values/color.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="white">#ffffffff</color>
  4. <color name="black">#ff000000</color>
  5. <color name="pink">#ffffcbd7</color>
  6. <color name="green">#ffbae4b6</color>
  7. </resources>

注意事项:

textview控件默认没有点击和焦点的,所以需要在布局文件中把clickable和focusable的值手动添加为true。

Demo下载:

网址:https://github.com/duhuan2015/TextViewTest    点击下载

TextView点击后背景颜色、文字颜色改变(转)的更多相关文章

  1. Android中通过xml改变背景及文字颜色

    原创文章,转载请注明出处,谢谢! 本篇主要介绍Android开发中,通过XML资源文件来设置控件在不同状态下的背景及文字颜色.关于xml改变背景及文字颜色的原理,大家可以去看一下郭霖大神的源码分析文章 ...

  2. 设置LISTControl控件某一行的背景和文字颜色

    定义宏 用listcontrol的SetItemData设置某一行的属性,通过自定义属性标识实现. 自定义某行内容颜色属性: #define COLOR_DEFAULT 0 //默认颜色 #defin ...

  3. button改变背景与文字颜色

    1.定义/zhsh/res/color/txt_guide_selector.xml <?xml version="1.0" encoding="utf-8&quo ...

  4. (转)TextView 设置背景和文本颜色的问题

    在做一个项目,突然遇到如下问题 比如:在color.xml中定义了几个颜色 <color name="white">#FFFFFF</color> < ...

  5. SecureCRT的背景和文字颜色的修改

    options->;session options->;emulation->;terminal选择linux(相应的服务器系统)ansi color 打上钩options-> ...

  6. android radiogroup样式(设置切换背景与文字颜色)

    main.xml <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_cont ...

  7. VC改变CListCtrl 表格中文字颜色,和背景颜色。

    (1)首先需要自定义一个类,派生自CListCtrl.如下图: (2)然后在派生类的头文件中声明一个成员函数,如下图: (3)在源文件中实现该成员方法,如图: (4)在源文件中做消息映射,如图: 这时 ...

  8. EditText 详细信息(监听事件时,输入改变、透明背景、提示改变文字颜色、密文输入)

    1.对EditText输入监视.给EditText 捆绑 addTextChangedListener 监控事件 能够. 2.EditText输入内容.密文显示: android:password=& ...

  9. android TextView 设置部分文字背景色和文字颜色

    通过SpannableStringBuilder来实现,它就像html里边的元素改变指定文字的文字颜色或背景色 public class MainActivity extends Activity { ...

随机推荐

  1. java this关键字的用法

  2. procixx 时钟的坑

    error 1.procixx设置800M,uboot中是720M 了解到整个过程是,当procixx配置后,通过FSBL中的ps_init.c反应出来 // ARM_PLL_FDIV = 48    ...

  3. Ehcahe独立使用

    <?xml version="1.0" encoding="utf-8"?><ehcache xmlns:xsi="http://w ...

  4. Flutter-AppBar

    1.1 简介 AppBar “应用栏” 应用栏由工具栏组成,或者是工具栏和其他 widget 组合形成,例如 TabBar和FlexibleSpaceBar; 应用栏通常用于 Scaffold.app ...

  5. SQL执行顺序和coalesce以及case when的用法

    1.mysql的执行顺序 from on join where group by having select distinct union   //UNION 操作符用于合并两个或多个 SELECT ...

  6. 1.xml简单介绍

    1.xml(eXtensible Markup Language)的简介 (1)xml: 可扩展标记型语言 - 标记型语言:例如html也是标记型语言,也是使用标签来操作的 - 可扩展:html里面的 ...

  7. hover()函数的用法

    定义和用法 hover() 方法规定当鼠标指针悬停在被选元素上时要运行的两个函数. 实例 当鼠标指针悬停在上面时,改变 <p> 元素的背景颜色: $("p").hove ...

  8. HTTP: 状态码200~505说明

    HTTP状态码(HTTP Status Code) 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释: 1xx(临时响应) 表示 ...

  9. boost multi array

    Boost MultiArray is a library that simplifies using arrays with multiple dimensions. 1. #include < ...

  10. BUUCTF | easy_tornado

    第一次遇到模板注入的题,赶紧记笔记,此篇相当于对大佬的做法的复现Orz,师傅太强了https://blog.csdn.net/weixin_44255856/article/details/97687 ...