View[4] View Drawable State

【参考链接】

Android状态系统——View状态设计http://www.jianshu.com/p/4c584f8474b3

说说Android中的touch modehttp://blog.****.net/myoungmeng/article/details/54023605

Android触摸模式(Touch Modehttp://blog.****.net/mcryeasy/article/details/53708493?utm_source=itdadao&utm_medium=referral

 

View当中有一个mViewFlags成员变量,该成员变量是一个32int类型,分别用其中的几位到几位来表示某项的状态

比如说第3~4位来表示Visibility3种状态:VISIBLEINVISIBLEGONE

View[4] View Drawable State


不过这里不研究这些全部。

其中的一些标志位,用于控制ViewDrawableState。主要有5State

WINDOW_FOCUSEDSELECTEDFOCUSEDENABLEDPRESSED

5State不是互斥,而是相互独立的。即某个时刻可能这5项中的某几项是被置位了

View[4] View Drawable State


这里主要研究后4State的含义。

当我们为一个Viewbackground设置StateDrawable的时候,他会将View当前StateStateDraw中的Item从下往下比较,只要item不是不符合(即子集),就会选用该item。所以注意书写顺序。

后续实验使用的StateDrawable代码如下

View[4] View Drawable State

SELECTED

相关方法:setSelected()isSelected()

当调用View.setSelected()的时候会置SELECTED位。

 

FOCUSED

相关方法:setFocusable()isFocusable()setFocusableInTouchMode()isFocusableInTouchMode()isFocused()requestFocus()

焦点的设计最初是用于非触摸屏的设备,像电视这种通过遥控按键、最早的HTC G1手机通过轨迹球,来进行移动选择。如果一个View可以获取到焦点,则可以移动到该View上去,并给用户以视觉反馈,如在周边显示一圈黄色的边框。

而对于触摸屏设备,不需要进行移动选择,直接通过触摸操作,也可以使某个View被点击时显示为获取到焦点的效果。

 

Xml中跟焦点相关的属性主要有3个:focusablefocusableInTouchModedescendantFocusability

 

focusable& focusableInTouchMode

1、当设置focusable=true时,并不影响focsuableInTouchMode,当设置focusable=false时,也会将focusableInTouchMode=false.

focusable

true

true

false

focusableInTouchMode

true

false

false


View[4] View Drawable State


View[4] View Drawable State


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

View[4] View Drawable State

 

以如下代码为例,LinearLayout中有一个TextViewEditText,其中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());
   
}
}

 

如果设置TextViewfocusable=true&focusableInTouchMode=true

View[4] View Drawable State


当进入Acivity时,进入InTouchMode模式,由TextView获取到焦点

View[4] View Drawable State

点击EditText时,由EditText获取到焦点

View[4] View Drawable State

点击向上按键,退出InTouchMode模式,向上由TextView获取到焦点

View[4] View Drawable State

点击TextView,进入InTouchMode模式,由于当前TextView已持有焦点,会触发onClick事件

View[4] View Drawable State 


如果设置TextViewfocusable=true&focusableInTouchMode=false

View[4] View Drawable State

进入Activity时,进入InTouchMode模式,由EditText获取到焦点

View[4] View Drawable State

点击向上按键,退出InTouchMode模式,向上由TextView获取到焦点

View[4] View Drawable State

点击TextView,进入InTouchMode模式,失去焦点,无焦点时直接触发onClick事件

View[4] View Drawable State

 

descendantFocusability

在沿视图树从上向下寻找View进行焦点派发的时候,还跟ViewGroupdescendantFocusability属性有关

View[4] View Drawable State

在上面例子的基础上,给外层的LinearLayout也设置如上的StateDrawable,并设置不同的descendantFocusability属性

(保持TextViewfocusable=true&focusableInTouchMode=true

 

beforeDescendants

afterDescendants

blocksDescendants

View[4] View Drawable State

View[4] View Drawable State

View[4] View Drawable State

寻找时先于子View返回

寻找时后于子View返回

默认就是这种

寻找时不继续向下寻找,会导致子View永远获取不到焦点

图中TextViewEditText都获取不到焦点,无法弹出输入法,即使requestFocus()也不行

 

ENABLED

相关方法:setEnabled()isEnabled()setClickable()setLongClickable()

setEnabled(false)会导致View.onTouchEvent()直接返回,不会进行后面的处理,即自定义View重写onTouchEvent()方法时调用super.onTouchEvent()无效果

并且,前面也说了是相互独立的,enable与否并不会对selectedfocused造成影响

View[4] View Drawable State

 

PRESSED

相关方法:setPressed()isPressed()

onTouchEventACTION_DOWN中会设置pressed并在ACTION_UP中取消

View[4] View Drawable State