Android状态check、focused、pressed、selected小结
Android状态check、focused、pressed、selected小结
在Android有些组件需要设置状态或判断状态,我们需要知道这些状态代表的意义。主要应用是在资源文件的使用。
一.在资源文件中常见状态代表的意义
(一)android:drawable
这个属性是必须的,为当前控件指定资源。
(二)android:state_pressed
布尔值。true指当用户点击或者触摸该控件的状态。默认为false;一般用于设置按钮颜色/图片的设置。
(三)android:state_focused
布尔值。ture指当前控件获得焦点时的状态。默认为false;一般用于EdiText。
(四)android:state_hovered
布尔值。true表示光标移动到当前控件上的状态。默认为false;光标是否悬停,通常与state_focused 相同,它是4.0的新特性,一般用于EdiText。
(五)android:state_selected
布尔值。true表示被选择的状态,例如在一个下拉列表中用方向键下选择其中一个选项。
这个和focus的区别,selected是focus不充分的情况。比如一个listview获得焦点(focus),而用方向键选择了其中的一个item(selected)
(六)android:state_checkable
布尔值。ture表示可以被勾选的状态。这个仅在当控件具有被勾选和不被勾选的状态间转换时才起作用。
(七)android:state_checked
布尔值。true表示当前控件处于被勾选(check的状态)
(八)android:state_enabled
布尔值。true表示当前控件出于可用的状态。比如可以被点击
(九)android:state_activated
布尔值。true表示当前控件被激活的状态。
android:state_window_focused
布尔值。true表示当前控件出于最前端时,应用窗口获得焦点的状态。
二.代码示例
(一)设置按钮在不同状态的改变
1.drawable资源文件
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_enabled="true" android:state_window_focused="false" android:drawable="@drawable/button_pressed" /> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <item android:state_hovered="true" android:drawable="@drawable/button_focused" /> <item android:drawable="@drawable/button_normal" /> selector>
2.资源文件的设置
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/button" />
下面几篇是我之前些的博客:
(二)自定义背景设置示例:
http://blog.csdn.net/wenzhi20102321/article/details/52685058
(三)点击效果示例:
http://blog.csdn.net/wenzhi20102321/article/details/52689585?locationNum=3&fps=1
(四)字体颜色选择器示例:
http://blog.csdn.net/wenzhi20102321/article/details/52776356
(五)高级控件之列表选择框(Spinner)有条目的选中,没有资源文件
http://blog.csdn.net/wenzhi20102321/article/details/53573475
更多相关文章
- android的map上的浮动菜单以及日历控件
- shape.gradient使用示例
- android:shape的使用 (android用xml文件生成图像控件)
- 【自定义控件】 ------- GridView
- 安卓隐藏标题栏状态栏 ,实现全屏效果
- android:Spinner(下拉框)控件的使用
- 基本控件学习以( RadioGroup和RadioButton 的学习使用)
- Android 获取控件宽高
- Android屏幕尺寸、标题栏高度、状态栏高度、当前View尺寸