selector

1.selector 从单词的意思来说:选择者,选择器,就是对你的目标的控制。

从API来说:

A controller for the selection of SelectableChannel objects. Selectable channels can be registered with a selector and get a SelectionKey that represents the registration. The keys are also added to the selector's key set. Selection keys can be canceled so that the corresponding channel is no longer registered with the selector.

By invoking the select method, the key set is checked and all keys that have been canceled since last select operation are moved to the set of canceled keys. During the select operation, the channels registered with this selector are checked to see whether they are ready for operation according to their interest set

其实API 说了那么就是在解释单词的意思。

selector主要是用在ListView的item单击样式和TextView和Button的点击样式。

2.现在对他的一些属性进行介绍

属性介绍:

android:state_selected选中

android:state_focused获得焦点

android:state_pressed点击

android:state_enabled设置是否响应事件,指所有事件

3.下面通过例子来说一下selector对TextView和ListView的设置:

TextView:

  1).在res下创建一个drawable文件夹用于装textselector.xml  ,对点击样式的定义放在这里面

  2).values下定义colors.xml文件,将颜色资源放在里面

  3).将textselector.xml资源加载到TextView上,

textselector.xml

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/red"></item> <!-- 点击后的字体颜色 -->
<item android:color="@color/gray"></item> <!-- 默认字体颜色 -->
</selector>

activity.xml

   <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="@drawable/textselector"
android:textSize="15dp"
/> <!-- textColor对 selector引用 -->

这样运行程序去点击textview是不会看到textview颜色改变的,这是因为还没有给TextView添加按钮监听事件,就算事件不取执行什么功能都必须去设置。

 TextView text = (TextView)findViewById(R.id.text);
text.setOnClickListener(null);

终上所述:就有效果了

下面会说说ListView:

(1)在ListView中添加如下属性代码

  1. android:listSelector="@drawable/mylist_view"
<android:listSelector="@drawable/mylist_view"  />

(2)在ListView的item界面中添加如下属性代码

  <android:background="@drawable/mylist_view" />

(3)利用JAVA代码直接编写

 Drawable drawable = getResources().getDrawable(R.drawable.mylist_view); 

 listView.setSelector(drawable);  
 
Button的类似就不说了。

android selector(如对TextView点击样式改变)的更多相关文章

  1. android TextView selector点击样式改变

    1.selector 从单词的意思来说:选择器,就是对你的目标的控制.selector主要是用在ListView的item单击样式和TextView和Button的点击样式. 2.主要属性介绍: an ...

  2. Android toolbar menu 字体点击样式

    今天在做toolbar的时候,右边的菜单的点击事件,就是文字,然后文字的样式,文字的大小,文字的颜色,高了半天.最后发现,文字点下去之后是有样式的,也就是按下去有阴影. 哥哥的耐心好,就知道这不是问题 ...

  3. Android TextView点击效果

    在Android开发中,我们有时候需要单独的点击某一段文本,如图所示: 如上图,我们要求点击新用户注册这个TextView,为了有更好的用户体验,我们肯定要设置该TextView的点击效果.下面介绍如 ...

  4. android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现

              android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现 首先看到selector的属性: android:state_focus ...

  5. android selector设置button点击效果(具体)以及常见问题

    button的点击效果学习起来其实比較easy,此点对开发人员来说也是使用的比較频繁的一个知识点,与它相关的还有编辑框的获取焦点时改变背景颜色.选择button选择时改变字体颜色等等.这些其实都是用到 ...

  6. [Android]TextView点击获取部分内容

    TextView控件本身有很多属性可以进行控制,如果要获取内容只需要getText()方法就可以实现,同时也可以为TextView设置各种监听器.但是,如果想要实现点击获取TextView内部的部分内 ...

  7. android selector详解

    --> 改变字体的颜色<selector xmlns:android="http://schemas.android.com/apk/res/android"> ...

  8. Android selector背景选择器

    selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中 android:state_focused--------获得焦点 ...

  9. Android 基础控件 TextView

    一TextView介绍: TextView是UI最基本的组件,使用TextView可以显示丰富的文本信息.设置添加TextView最常见的方法就是在xml中添加TextView元素,并指定属性.Tex ...

随机推荐

  1. Hibernate一对多(多对一)关联关系

    上一篇博文总结了 Hibernate 的一对一的关联关系, 包括基于主键的单向一对一, 基于外键的单向一对一, 基于外键的双向一对一. 下面咱们说一下 Hibernate 的一对多关联关系. 其实一对 ...

  2. 关于 NPOI 报 Invalid column index (256). Allowable column range for BIFF8 is (0..255) or ('A'..'IV') 错误的解决办法

    当看到这个错误的时候,网上搜索可以会有些说列数有限制之类的说法,这个说法是相对于 Office 2003 的,在 Office 2007 之前,最多只可以创建  列:在 Office 2007 之后, ...

  3. asp.net截取指定长度的字符串内容

    /// <summary> /// 用于截取指定长度的字符串内容 /// </summary> /// <param name="sString"&g ...

  4. 编程:什么是REST

    REST (REpresentation State Transfer) 描述了一个架构样式的网络系统,比如 web 应用程序.它首次出现在 2000 年 Roy Fielding 的博士论文中,他是 ...

  5. POJ 2398 - Toy Storage 点与直线位置关系

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5439   Accepted: 3234 Descr ...

  6. Spring MVC常见的三种URL请求参数的处理

    //方法1:@RequestParam //url:/test1?id=123 @RequestMapping(value = "/test1",method = RequestM ...

  7. 简单的python服务器程序

    一个接受telnet输入的服务器端小程序 #!/usr/local/bin/python3.5 #coding:utf-8 import socket host = '' port = 51423 s ...

  8. 基于Java的WebSocket推送【转载】

    http://www.cnblogs.com/tonyY/p/5495417.html

  9. iOS工作笔记(十四)

    1.scrollview的frame指的是其可视范围,contentSize指的是其滚动范围,分别是在水平方向和竖直方向上的 所以要让scrollview在水平方向不能滚动,那么需要如下设置 _scr ...

  10. strace命令介绍(转)

    原文链接:http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316692.html 简介 strace常用来跟踪进程执行时的系统调用和所接收的信 ...