android selector(如对TextView点击样式改变)
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中添加如下属性代码
- 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);
android selector(如对TextView点击样式改变)的更多相关文章
- android TextView selector点击样式改变
1.selector 从单词的意思来说:选择器,就是对你的目标的控制.selector主要是用在ListView的item单击样式和TextView和Button的点击样式. 2.主要属性介绍: an ...
- Android toolbar menu 字体点击样式
今天在做toolbar的时候,右边的菜单的点击事件,就是文字,然后文字的样式,文字的大小,文字的颜色,高了半天.最后发现,文字点下去之后是有样式的,也就是按下去有阴影. 哥哥的耐心好,就知道这不是问题 ...
- Android TextView点击效果
在Android开发中,我们有时候需要单独的点击某一段文本,如图所示: 如上图,我们要求点击新用户注册这个TextView,为了有更好的用户体验,我们肯定要设置该TextView的点击效果.下面介绍如 ...
- android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现
android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现 首先看到selector的属性: android:state_focus ...
- android selector设置button点击效果(具体)以及常见问题
button的点击效果学习起来其实比較easy,此点对开发人员来说也是使用的比較频繁的一个知识点,与它相关的还有编辑框的获取焦点时改变背景颜色.选择button选择时改变字体颜色等等.这些其实都是用到 ...
- [Android]TextView点击获取部分内容
TextView控件本身有很多属性可以进行控制,如果要获取内容只需要getText()方法就可以实现,同时也可以为TextView设置各种监听器.但是,如果想要实现点击获取TextView内部的部分内 ...
- android selector详解
--> 改变字体的颜色<selector xmlns:android="http://schemas.android.com/apk/res/android"> ...
- Android selector背景选择器
selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中 android:state_focused--------获得焦点 ...
- Android 基础控件 TextView
一TextView介绍: TextView是UI最基本的组件,使用TextView可以显示丰富的文本信息.设置添加TextView最常见的方法就是在xml中添加TextView元素,并指定属性.Tex ...
随机推荐
- SQL SERVER连接、合并查询
----创建测试表MyStudentInfoCREATE table MyStudentInfo( Id int not null primary key, Name varchar(16), ...
- Linux内核分析第五周学习总结:扒开系统调用的三层皮(下)
韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.给MenuO ...
- LVS集群类型
一.LVS-NAT 简述:多目标的DNAT,通过Director修改请求报文中的目标地址和端口为LVS挑选出来的某RS的RIP和PORT实现转发 特点: (1)RIP和DIP必须在同一网络,且应该使用 ...
- Server Apache Tomcat v7.0 at localhost failed to start.
自己在学习servlet中,突然启动不了Tomcat7服务器,提示出现如下错误: 百度之后,没有找到解决办法,偶然发现是我的Web-content下--WEB-INF下--web.xml的配置文件内 ...
- windowsAPI popup trace tip(toolTip)
class UIHELPER_EXPORT ToolTipWindow : public chMessageHandler{ DECLARE_PROCESS_OBJECT(ToolTipWindow) ...
- strace命令介绍(转)
原文链接:http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316692.html 简介 strace常用来跟踪进程执行时的系统调用和所接收的信 ...
- 【随笔】内存 & I/O检测相关
缺页中断 缺页中断属于内部中断,也就是异常.细分的话属于异常中的故障. 在执行一条指令时,如果发现他要访问的页没有在内存中(存在位为0),那么停止该指令的执行,并产生一个页不存在异常,对应的故障处理程 ...
- Delphi inline编译器优化问题
function Test():Integer; inline; var P:Pointer; begin FreeMem(P); Result := AtomicIncrement(__gr); / ...
- 21: Arithmetic Sequence--HZAU(dp)
http://acm.hzau.edu.cn/problem.php?id=21 题目大意: 给你一个序列问在数字最多的等比数列 分析: 刚开始看到题就知道是一个dp但是我dp实在是渣到不行 后来发 ...
- 多重背包 (poj 1014)
题目:Dividing 题意:6种重量的的石头,每个给定数量,用总重的一半去装,问能否装满. #include <iostream> #include <algorithm> ...