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. 控件的相对位置与绝对位置-UI界面编辑器(SkinStudio)教程

    绝对位置: 相对位置: 相对位置配合Anchor属性使用 例如Anchor属性选择left|top(即相对位置的left和top保持不变) 窗口大小改变前: 窗口大小改变后: 可以看到控件相对位置的l ...

  2. 关于ES6的数组字符串方法

    注:ES6的一些新属性会显示语法错误,不过不会影响效果,在Languages里面也可以调: let:用来定义变量 特点:只能在代码块里面使用,let拥有块级作用域;并且let不允许重复声明;比如: v ...

  3. .Net的错误机制

    //优先级1    protected void Page_Error(object sender, EventArgs e)     {        Exception objErr = Serv ...

  4. WordPress建站指南

    WordPress建站指南(1)   写在前面: 3月份用10天零碎时间火速完成了建站,后台95%的工作都交给了WP(WordPress).如果想偷懒的话,WP是一个绝好的选择,估计有个小半天就建完收 ...

  5. jQuery数组处理汇总

    jQuery数组处理汇总   有段时间没写什么了, 打算把jquery中的比较常用的数组处理方法汇总一下 $.each(array, [callback])遍历,很常用 1 2 3 4 5 6 7 8 ...

  6. AsyncTask下载网络图片的简单应用

    1.imageTest package lpc.com.asynctaskdemo; import android.app.Activity; import android.graphics.Bitm ...

  7. SegmentFault创始人高阳:辍学后带着500元北漂,4年建成国内最大开发者

    i黑马注:i黑马曾经和高阳聊过几次天,在他身上我看到了90后CEO特别明显的成功特质“敢为天下先”.在别人犹豫的时候敢第一个出手,在互联网时代往往会取得最关键的“先机优势”. 7月19日,“腾讯产品家 ...

  8. CMS为什么采用“标记-清除”算法

    分代式GC里,年老代常用mark-sweep:或者是mark-sweep/mark-compact的混合方式,一般情况下用mark-sweep,统计估算碎片量达到一定程度时用mark-compact. ...

  9. sublime text3 编译less

    1,  下载 插件   链接:http://pan.baidu.com/s/1bNbFJK 密码:m3zt 2,解压后把lessc文件夹放到sublime text3 的\Data\Packages ...

  10. POJ 3686 The Windy's 最小费用最大流

    每个工厂拆成N个工厂,费用分别为1~N倍原费用. //#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...