我们先从一个例子看起,先重写一个MyButton 继承Button,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
public
class
MyBtn
extends
Button {
public
MyButton(Context context) {
super
(context);
}
public
MyBtn(Context context, AttributeSet attrs) {
super
(context, attrs);
}
public
MyBtn(Context context, AttributeSet attrs,
int
defStyleAttr) {
super
(context, attrs, defStyleAttr);
}
@Override
public
boolean
dispatchTouchEvent(MotionEvent event) {
switch
(event.getAction()) {
case
MotionEvent.ACTION_DOWN:
MyLog.e(
"dispatchTouchEvent====MyButton=====ACTION_DOWN"
);
break
;
case
MotionEvent.ACTION_MOVE:
MyLog.e(
"dispatchTouchEvent====MyButton=====ACTION_MOVE"
);
break
;
case
MotionEvent.ACTION_UP:
MyLog.e(
"dispatchTouchEvent====MyButton=====ACTION_UP"
);
break
;
}
return
super
.dispatchTouchEvent(event);
}
@Override
public
boolean
onTouchEvent(MotionEvent event) {
switch
(event.getAction()) {
case
MotionEvent.ACTION_DOWN:
MyLog.e(
"onTouchEvent====MyButton=====ACTION_DOWN"
);
break
;
case
MotionEvent.ACTION_MOVE:
MyLog.e(
"onTouchEvent====MyButton=====ACTION_MOVE"
);
break
;
case
MotionEvent.ACTION_UP:
MyLog.e(
"onTouchEvent====MyButton=====ACTION_UP"
);
break
;
}
return
super
.onTouchEvent(event);
}
|
布局文件如下:
1
2
3
4
5
|
<relativelayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
android:paddingbottom=
"@dimen/activity_vertical_margin"
android:paddingleft=
"@dimen/activity_horizontal_margin"
android:paddingright=
"@dimen/activity_horizontal_margin"
android:paddingtop=
"@dimen/activity_vertical_margin"
tools:context=
".MainActivity"
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
>
<com.xjp.testtouchevent.mybutton android:id=
"@+id/myButton"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"测试"
>
</com.xjp.testtouchevent.mybutton></relativelayout>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public
class
MainActivity
extends
ActionBarActivity {
private
Button myButton;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnTouchListener(
new
View.OnTouchListener() {
@Override
public
boolean
onTouch(View v, MotionEvent event) {
switch
(event.getAction()) {
case
MotionEvent.ACTION_DOWN:
MyLog.e(
"onTouch====MyButton=====ACTION_DOWN"
);
break
;
case
MotionEvent.ACTION_MOVE:
MyLog.e(
"onTouch====MyButton=====ACTION_MOVE"
);
break
;
case
MotionEvent.ACTION_UP:
MyLog.e(
"onTouch====MyButton=====ACTION_UP"
);
break
;
}
return
false
;
}
});
myButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
MyLog.e(
"onClick====MyButton=====onClick"
);
}
});
}
}
|
我们从打印结果可以直观看到,点击Button按钮事件分发过程如下 dispatchTouchEvent---->onTouch---->onTouchEvent----->onClick。并且如果仔细的你会发现,都是在ACTION_UP事件之后才触发onClick点击事件,为什么会是这样??现在我们不得而知。我们仅仅是从打印结果推测事件分发的结论,现在我们从源码分析下这个事件分发流程为什么是这样子。
事件分发都是从dispatchTouchEvent方法开始的,那么我们这里是重写了dispatchTouchEvent方法,并且最后也调用了父类的super.dispatchTouchEvent(event)方法。那么我们看看父类中的方法到底做了什么??点击进入父类的dispatchTouchEvent方法,发现此方法在View类中找到,其实也不奇怪,所有控件的父类都是View。这里我贴出最新源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public
boolean
dispatchTouchEvent(MotionEvent event) {
boolean
result =
false
;
if
(mInputEventConsistencyVerifier !=
null
) {
mInputEventConsistencyVerifier.onTouchEvent(event,
0
);
}
final
int
actionMasked = event.getActionMasked();
if
(actionMasked == MotionEvent.ACTION_DOWN) {
// Defensive cleanup for new gesture
stopNestedScroll();
}
if
(onFilterTouchEventForSecurity(event)) {
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if
(li !=
null
&& li.mOnTouchListener !=
null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(
this
, event)) {
result =
true
;
}
if
(!result && onTouchEvent(event)) {
result =
true
;
}
}
if
(!result && mInputEventConsistencyVerifier !=
null
) {
mInputEventConsistencyVerifier.onUnhandledEvent(event,
0
);
}
// Clean up after nested scrolls if this is the end of a gesture;
// also cancel it if we tried an ACTION_DOWN but we didn't want the rest
// of the gesture.
if
(actionMasked == MotionEvent.ACTION_UP ||
actionMasked == MotionEvent.ACTION_CANCEL ||
(actionMasked == MotionEvent.ACTION_DOWN && !result)) {
stopNestedScroll();
}
return
result;
}
|
忽略其他无关代码,我们直接看17--25行。第17行的if判断关键在于li.mOnTouchListener.onTouch(this, event) 的返回值,这个接口回调就是我们外面写的myButton.setOnTouchListener事件(Button 的onTouch事件),在MainActivity代码里,我们setOnTouchListener返回的值是false,所以在源码中我们可以看到 17行的条件不成立,那么条件不成立,result=false;因此,源码的第23行if 判断第一个条件成立,继续执行第二个条件,也就是onTouchEvent。我们跳到这个方法里看看里面干啥了?看如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
public
boolean
onTouchEvent(MotionEvent event) {
if
(((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
switch
(event.getAction()) {
case
MotionEvent.ACTION_UP:
boolean
prepressed = (mPrivateFlags & PFLAG_PREPRESSED) !=
0
;
if
((mPrivateFlags & PFLAG_PRESSED) !=
0
|| prepressed) {
// take focus if we don't have it already and we should in
// touch mode.
boolean
focusTaken =
false
;
if
(isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if
(prepressed) {
// The button is being released before we actually
// showed it as pressed. Make it show the pressed
// state now (before scheduling the click) to ensure
// the user sees it.
setPressed(
true
, x, y);
}
if
(!mHasPerformedLongPress) {
// This is a tap, so remove the longpress check
removeLongPressCallback();
// Only perform take click actions if we were in the pressed state
if
(!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if
(mPerformClick ==
null
) {
mPerformClick =
new
PerformClick();
}
if
(!post(mPerformClick)) {
performClick();
}
}
}
if
(mUnsetPressedState ==
null
) {
mUnsetPressedState =
new
UnsetPressedState();
}
if
(prepressed) {
postDelayed(mUnsetPressedState,
ViewConfiguration.getPressedStateDuration());
}
else
if
(!post(mUnsetPressedState)) {
// If the post failed, unpress right now
mUnsetPressedState.run();
}
removeTapCallback();
}
break
;
return
true
;
}
return
false
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
boolean
performClick() {
final
boolean
result;
final
ListenerInfo li = mListenerInfo;
if
(li !=
null
&& li.mOnClickListener !=
null
) {
playSoundEffect(SoundEffectConstants.CLICK);
li.mOnClickListener.onClick(
this
);
result =
true
;
}
else
{
result =
false
;
}
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
return
result;
}
|
现在我们来看看其他情况:当onTouch返回为true,打印结果如下:
惊奇的发现,竟然没有执行onClick事件是吧????如果你仔细阅读上面的文章,估计你知道为什么了吧?还是跟大家一起分析一下吧:源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public
boolean
dispatchTouchEvent(MotionEvent event) {
boolean
result =
false
;
if
(mInputEventConsistencyVerifier !=
null
) {
mInputEventConsistencyVerifier.onTouchEvent(event,
0
);
}
final
int
actionMasked = event.getActionMasked();
if
(actionMasked == MotionEvent.ACTION_DOWN) {
// Defensive cleanup for new gesture
stopNestedScroll();
}
if
(onFilterTouchEventForSecurity(event)) {
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if
(li !=
null
&& li.mOnTouchListener !=
null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(
this
, event)) {
result =
true
;
}
if
(!result && onTouchEvent(event)) {
result =
true
;
}
}
if
(!result && mInputEventConsistencyVerifier !=
null
) {
mInputEventConsistencyVerifier.onUnhandledEvent(event,
0
);
}
// Clean up after nested scrolls if this is the end of a gesture;
// also cancel it if we tried an ACTION_DOWN but we didn't want the rest
// of the gesture.
if
(actionMasked == MotionEvent.ACTION_UP ||
actionMasked == MotionEvent.ACTION_CANCEL ||
|