这篇在测试(一)的基础上测试ButtonExt
http://blog.csdn.net/id19870510/article/details/46381051
主界面的整体Layout布局如下(TextViewExt改成ButtonExt 没有重新设置background属性)
<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:gravity="center">
<miss.goddess.touchdispatch.view.ButtonExt
android:layout_width="100dip"
android:layout_height="100dip"
//android:background="#ccc" 注释掉
android:padding="50dip"
android:text="@string/hello_world"/>
</LinearLayout>
--------------------------------------------------------------------------------
Activity覆盖了两个方法
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.d("touch:activity-dispatch", EventName.getName(event));
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("touch:activity-onTouch", EventName.getName(event));
return super.onTouchEvent(event);
}
ButtonExt主要覆盖了 dispatchTouchEvent onTouchEvent方法
public class ButtonExt extends Button {
。。。。。。。。。。。。
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.d("touch:view:dispatch", EventName.getName(event));
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("touch:view:onTouch", EventName.getName(event));
return super.onTouchEvent(event);
}
}
开始测试
-----------------------------------------------------------------------------------------------------
分两种情况触摸屏幕
1、 滑动轨迹【A-O-B】
打印的日志如下
06-04 14:40:03.950: D/touch:activity-dispatch(14390): ACTION_DOWN---
06-04 14:40:03.960: D/touch:activity-onTouch(14390): ACTION_DOWN---
06-04 14:40:04.060: D/touch:activity-dispatch(14390): ACTION_MOVE
06-04 14:40:04.060: D/touch:activity-onTouch(14390): ACTION_MOVE
。。。。。。。。。。。。。。。。。。。。(省略)
06-04 14:40:04.550: D/touch:activity-dispatch(14390): ACTION_MOVE
06-04 14:40:04.550: D/touch:activity-onTouch(14390): ACTION_MOVE
06-04 14:40:04.600: D/touch:activity-dispatch(14390): ACTION_UP
06-04 14:40:04.600: D/touch:activity-onTouch(14390): ACTION_UP
可以发现。ButtonExt的dispatchTouchEvent onTouch事件根本不会执行
在滑动的过程中也能看到button按钮根本没有按下的selector效果
2、 滑动轨迹【O-B】
06-08 15:52:07.740: D/touch:activity-dispatch(19221): ACTION_DOWN---
06-08 15:52:07.740: D/touch:view:dispatch(19221): ACTION_DOWN---
06-08 15:52:07.740: D/touch:view:touch(19221): ACTION_DOWN---
06-08 15:52:07.980: D/touch:activity-dispatch(19221): ACTION_MOVE
06-08 15:52:07.980: D/touch:view:dispatch(19221): ACTION_MOVE
06-08 15:52:07.980: D/touch:view:touch(19221): ACTION_MOVE
06-08 15:52:08.000: D/touch:activity-dispatch(19221): ACTION_MOVE
06-08 15:52:08.000: D/touch:view:dispatch(19221): ACTION_MOVE
06-08 15:52:08.000: D/touch:view:touch(19221): ACTION_MOVE
。。。。。。手指划出button区域以后,ButtonExt仍然能接收screen touch 事件
06-08 15:52:12.590: D/touch:activity-dispatch(19221): ACTION_MOVE
06-08 15:52:12.590: D/touch:view:dispatch(19221): ACTION_MOVE
06-08 15:52:12.590: D/touch:view:touch(19221): ACTION_MOVE
。。。。。。。
06-08 15:52:12.640: D/touch:activity-dispatch(19221): ACTION_MOVE
06-08 15:52:12.640: D/touch:view:dispatch(19221): ACTION_MOVE
06-08 15:52:12.640: D/touch:view:touch(19221): ACTION_MOVE
06-08 15:52:12.660: D/touch:activity-dispatch(19221): ACTION_MOVE
06-08 15:52:12.660: D/touch:view:dispatch(19221): ACTION_MOVE
06-08 15:52:12.660: D/touch:view:touch(19221): ACTION_MOVE
06-08 15:52:12.670: D/touch:activity-dispatch(19221): ACTION_MOVE
06-08 15:52:12.670: D/touch:view:dispatch(19221): ACTION_MOVE
06-08 15:52:12.670: D/touch:view:touch(19221): ACTION_MOVE
06-08 15:52:12.670: D/touch:activity-dispatch(19221): ACTION_UP
06-08 15:52:12.670: D/touch:view:dispatch(19221): ACTION_UP
06-08 15:52:12.670: D/touch:view:touch(19221): ACTION_UP
ButtonExt可以在dispatchTouchEvent onTouchEvent中接收到Action_Down ACTION_MOVE ACTION_UP事件
activity的onTouchEvent事件收不到。。被中断了?
其中事件分发的顺序是
Activity dispatchTouchEvent -- > view dispatchTouchEvent -- > view onTouchEvent