android selector(如对TextView点击样式改变) - perfect亮
来源:互联网 发布:阿里云rds创建数据库 编辑:程序博客网 时间:2024/06/05 21:07
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
1 <?xml version="1.0" encoding="utf-8"?>2 <selector xmlns:android="http://schemas.android.com/apk/res/android">3 <item android:state_pressed="true" android:color="@color/red"></item> <!-- 点击后的字体颜色 -->4 <item android:color="@color/gray"></item> <!-- 默认字体颜色 -->5 </selector>
activity.xml
复制代码
1 <TextView2 android:id="@+id/text"3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content"5 android:text="@string/hello_world" 6 android:textColor="@drawable/textselector" 7 android:textSize="15dp"8 /> <!-- textColor对 selector引用 -->
复制代码
这样运行程序去点击textview是不会看到textview颜色改变的,这是因为还没有给TextView添加按钮监听事件,就算事件不取执行什么功能都必须去设置(或者给TextView设置属性android:clickable = true,也可以)
1 TextView text = (TextView)findViewById(R.id.text);
2 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代码直接编写
1 Drawable drawable = getResources().getDrawable(R.drawable.mylist_view); 2 3 listView.setSelector(drawable);
Button的类似就不说了。
- android selector(如对TextView点击样式改变) - perfect亮
- android TextView selector点击样式改变
- 安卓开发——通过selector实现对TextView点击样式改变
- textView结合selector点击改变文字颜色
- 点击LinearLayout使用selector改变TextView字体颜色
- 点击LinearLayout使用selector改变TextView字体颜色
- 点击LinearLayout使用selector改变TextView字体颜色
- 点击LinearLayout使用selector改变TextView字体颜色
- 点击LinearLayout使用selector改变TextView字体颜色 (转)
- 点击LinearLayout使用selector改变TextView字体颜色
- 点击LinearLayout使用selector改变TextView字体颜色
- android中为TextView动态改变字体颜色,如点击时更改
- android 设置textview获取焦点改变样式
- selector 点击样式
- android selector(改变button,textview,listview的背景图片)
- Android设置TextView的Selector来控制点击的颜色
- Android设置TextView的Selector来控制点击的颜色
- Android设置TextView的Selector来控制点击的颜色
- Unity3D中常用的数据结构总结与分析
- HttpUrlConnection
- 剑指Offer面试题42:翻转单词顺序;左旋转字符串 Java实现
- 网络html源码更改
- pullToRefresh
- android selector(如对TextView点击样式改变) - perfect亮
- Java关系运算符
- 深入浅出聊优化:从Draw Calls到GC
- 计算机网络
- Python与图像处理1
- 视频编程作业-两个有序列表的合并
- 全局变量和局部变量
- SpringMvc中使用模板绑定
- Ubuntu下设置环境变量PATH.doc