View[4] View Drawable State
【参考链接】
Android状态系统——View状态设计http://www.jianshu.com/p/4c584f8474b3
说说Android中的touch modehttp://blog.****.net/myoungmeng/article/details/54023605
Android触摸模式(Touch Mode)http://blog.****.net/mcryeasy/article/details/53708493?utm_source=itdadao&utm_medium=referral
View当中有一个mViewFlags成员变量,该成员变量是一个32位int类型,分别用其中的几位到几位来表示某项的状态
比如说第3~4位来表示Visibility的3种状态:VISIBLE、INVISIBLE、GONE。
不过这里不研究这些全部。
其中的一些标志位,用于控制View的Drawable的State。主要有5项State:
WINDOW_FOCUSED、SELECTED、FOCUSED、ENABLED、PRESSED。
这5项State不是互斥,而是相互独立的。即某个时刻可能这5项中的某几项是被置位了。
这里主要研究后4项State的含义。
当我们为一个View的background设置StateDrawable的时候,他会将View当前State跟StateDraw中的Item从下往下比较,只要item不是不符合(即子集),就会选用该item。所以注意书写顺序。
后续实验使用的StateDrawable代码如下
SELECTED
相关方法:setSelected()、isSelected()
当调用View.setSelected()的时候会置SELECTED位。
FOCUSED
相关方法:setFocusable()、isFocusable()、setFocusableInTouchMode()、isFocusableInTouchMode()、isFocused()、requestFocus()
焦点的设计最初是用于非触摸屏的设备,像电视这种通过遥控按键、最早的HTC G1手机通过轨迹球,来进行移动选择。如果一个View可以获取到焦点,则可以移动到该View上去,并给用户以视觉反馈,如在周边显示一圈黄色的边框。
而对于触摸屏设备,不需要进行移动选择,直接通过触摸操作,也可以使某个View被点击时显示为获取到焦点的效果。
Xml中跟焦点相关的属性主要有3个:focusable、focusableInTouchMode、descendantFocusability
focusable& focusableInTouchMode
1、当设置focusable=true时,并不影响focsuableInTouchMode,当设置focusable=false时,也会将focusableInTouchMode=false.
focusable |
true |
true |
false |
focusableInTouchMode |
true |
false |
false |
2、
对于非触摸设备,只有非TouchMode模式,对于触摸屏手机,当打开一个Activity的时候,会自动进入到TouchMode模式。当点击按键时(模拟器右侧的虚拟实体按键),则会退出TouchMode模式。
在如下3种情况下会从视图树中寻找一个新的View来分派焦点:(下面发生点击时也会去获取焦点)
1) 打开一个Activity时
2) 进入非TouchMode模式时
3) 在非TouchMode模式下点击按键进行移动时
在非TouchMode下,只要focusable=true就有资格获得焦点。在InTouchMode下,只有focusable=true并且focusableInTouchMode=true才有资格获得焦点。
3、如果当前View可以获得焦点但没有持有焦点,当点击时只会去获取焦点,并不会执行onClick,(在获得焦点后)再次点击才会执行onClick
如果不可以获得焦点则直接触发onClick
以如下代码为例,LinearLayout中有一个TextView和EditText,其中TextView设置了StateDrawable,
<?xml
version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.shadowfaxghh.demo.MainActivity">
<com.example.shadowfaxghh.demo.MyTextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello
World!"
android:textColor="#FFFFFF"
android:background="@drawable/bg"
android:layout_margin="3dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:enabled="true"
android:clickable="true"
android:onClick="click"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public classMainActivityextendsActivity
{
privateTextViewtv;
@Override
protected voidonCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.tv);
tv.setOnFocusChangeListener(newView.OnFocusChangeListener()
{
@Override
public voidonFocusChange(View
view, booleanb) {
Log.e("state","onFocusChange()
isInTouchMode="+view.isInTouchMode() +" focused="+b);
}
});
}
public voidclick(View
view){
Log.e("state","click()
isInTouchMode="+view.isInTouchMode());
}
}
如果设置TextView的focusable=true&focusableInTouchMode=true
当进入Acivity时,进入InTouchMode模式,由TextView获取到焦点
点击EditText时,由EditText获取到焦点
点击向上按键,退出InTouchMode模式,向上由TextView获取到焦点
点击TextView,进入InTouchMode模式,由于当前TextView已持有焦点,会触发onClick事件
如果设置TextView的focusable=true&focusableInTouchMode=false
进入Activity时,进入InTouchMode模式,由EditText获取到焦点
点击向上按键,退出InTouchMode模式,向上由TextView获取到焦点
点击TextView,进入InTouchMode模式,失去焦点,无焦点时直接触发onClick事件
descendantFocusability
在沿视图树从上向下寻找View进行焦点派发的时候,还跟ViewGroup的descendantFocusability属性有关
在上面例子的基础上,给外层的LinearLayout也设置如上的StateDrawable,并设置不同的descendantFocusability属性
(保持TextView为focusable=true&focusableInTouchMode=true)
beforeDescendants |
afterDescendants |
blocksDescendants |
|
|
|
寻找时先于子View返回 |
寻找时后于子View返回 默认就是这种 |
寻找时不继续向下寻找,会导致子View永远获取不到焦点 图中TextView和EditText都获取不到焦点,无法弹出输入法,即使requestFocus()也不行 |
ENABLED
相关方法:setEnabled()、isEnabled()、setClickable()、setLongClickable()
setEnabled(false)会导致View.onTouchEvent()直接返回,不会进行后面的处理,即自定义View重写onTouchEvent()方法时调用super.onTouchEvent()无效果
并且,前面也说了是相互独立的,enable与否并不会对selected、focused造成影响
PRESSED
相关方法:setPressed()、isPressed()
在onTouchEvent的ACTION_DOWN中会设置pressed并在ACTION_UP中取消