1.介绍
我们定义MyButton继承Button
package com.miyapay.cspm.test01.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;
/**
* Created by miya96 on 2016/12/2.
*/
public class MyButton extends Button {
private static final String TAG = MyButton.class.getSimpleName();
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "onTouchEvent ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG, "onTouchEvent ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "onTouchEvent ACTION_UP");
break;
default:
break;
}
return super.onTouchEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "dispatchTouchEvent ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG, "dispatchTouchEvent ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "dispatchTouchEvent ACTION_UP");
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
}
在onTouchEvent和dispatchTouchEvent中打印了日志
我们加入到主布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.miyapay.cspm.test01.view.MyButton
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="106dp"
android:text="@string/button"/>
</RelativeLayout>
MyButtonActivity代码
package com.miyapay.cspm.test01;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.miyapay.cspm.test01.view.MyButton;
/**
* Created by miya96 on 2016/12/2.
*/
public class MyButtonActivity extends Activity {
protected static final String TAG = "MyButton";
private MyButton mMyButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_activity);
mMyButton = (MyButton) findViewById(R.id.mybutton);
mMyButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "onTouch ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG, "onTouch ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "onTouch ACTION_UP");
break;
default:
break;
}
return false;
}
});
}
}
点击按钮
12-05 16:22:16.985 21852-21852/com.miyapay.cspm.test01 E/MyButton: dispatchTouchEvent ACTION_DOWN
12-05 16:22:16.985 21852-21852/com.miyapay.cspm.test01 E/MyButton: onTouch ACTION_DOWN
12-05 16:22:16.985 21852-21852/com.miyapay.cspm.test01 E/MyButton: onTouchEvent ACTION_DOWN
12-05 16:22:17.056 21852-21852/com.miyapay.cspm.test01 E/MyButton: dispatchTouchEvent ACTION_MOVE
12-05 16:22:17.057 21852-21852/com.miyapay.cspm.test01 E/MyButton: onTouch ACTION_MOVE
12-05 16:22:17.057 21852-21852/com.miyapay.cspm.test01 E/MyButton: onTouchEvent ACTION_MOVE
12-05 16:22:17.080 21852-21852/com.miyapay.cspm.test01 E/MyButton: dispatchTouchEvent ACTION_UP
12-05 16:22:17.080 21852-21852/com.miyapay.cspm.test01 E/MyButton: onTouch ACTION_UP
12-05 16:22:17.081 21852-21852/com.miyapay.cspm.test01 E/MyButton: onTouchEvent ACTION_UP
结果:
不论down、move、up
执行的顺序:
1.dispatchTouchEvent
2.setOntouchListenter的ontouch
3.onTouchEvent
源码探索
2.dispatchTouchEvent的源码
public boolean dispatchTouchEvent(MotionEvent event) {
if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
mOnTouchListener.onTouch(this, event)) {
return true;
}
return onTouchEvent(event);
}
1.首先进行了一个判断mOnTouchListener != null,(mViewFlags & ENABLED_MASK) == ENABLED以及mOnTouchListener.onTouch(this, event)三个条件为真,返回true,否则就执行onTouchEvent(event)方法并返回.
- 1.先看第一个条件
public void setOnTouchListener(OnTouchListener l) {
mOnTouchListener = l;
}
当我们给控件注册ontouch事件,mOnTouchListener 已经被赋值了
- 2.第二个条件(mViewFlags & ENABLED_MASK) == ENABLED是判断当前点击的控件是否是enable的,按钮默认都是enable的,因此这个条件恒定为true。
- 3.mOnTouchListener.onTouch(this, event),如果ontouch方法返回true,那么三个条件全部成立,整个方法返回true,如果我们在ontouch返回false
因此:首先在dispatchTouchEvent执行的事onTouch方法,因此ontouch肯定要优先于onClick方法,如果ontouch返回true,而dispatchTouchEvent返回true,不会继续执行.结果打印也证实了,如果ontouch返回true,onclick不会执行.
onTouchEvent
view的onTouchEvent分析
/**
* Implement this method to handle touch screen motion events.
*
* @param event The motion event.
* @return True if the event was handled, false otherwise.
*/
public boolean onTouchEvent(MotionEvent event) {
final int viewFlags = mViewFlags;
if ((viewFlags & ENABLED_MASK) == DISABLED) {
// A disabled view that is clickable still consumes the touch
// events, it just doesn't respond to them.
return (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
}
if (mTouchDelegate != null) {
if (mTouchDelegate.onTouchEvent(event)) {
return true;
}
}
if (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
if ((mPrivateFlags & 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 (!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) {
mPrivateFlags |= PRESSED;
refreshDrawableState();
postDelayed(mUnsetPressedState,
ViewConfiguration.getPressedStateDuration());
} else if (!post(mUnsetPressedState)) {
// If the post failed, unpress right now
mUnsetPressedState.run();
}
removeTapCallback();
}
break;
case MotionEvent.ACTION_DOWN:
if (mPendingCheckForTap == null) {
mPendingCheckForTap = new CheckForTap();
}
mPrivateFlags |= PREPRESSED;
mHasPerformedLongPress = false;
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
break;
case MotionEvent.ACTION_CANCEL:
mPrivateFlags &= ~PRESSED;
refreshDrawableState();
removeTapCallback();
break;
case MotionEvent.ACTION_MOVE:
final int x = (int) event.getX();
final int y = (int) event.getY();
// Be lenient about moving outside of buttons
int slop = mTouchSlop;
if ((x < 0 - slop) || (x >= getWidth() + slop) ||
(y < 0 - slop) || (y >= getHeight() + slop)) {
// Outside button
removeTapCallback();
if ((mPrivateFlags & PRESSED) != 0) {
// Remove any future long press/tap checks
removeLongPressCallback();
// Need to switch from pressed to not pressed
mPrivateFlags &= ~PRESSED;
refreshDrawableState();
}
}
break;
}
return true;
}
return false;
}
如果控件可以点击,进入到switch语句中,如果当前是抬起手指,则会进入到MotionEvent.ACTION_UP,经过判断会执行到performClick();方法
public boolean performClick() {
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
if (mOnClickListener != null) {
playSoundEffect(SoundEffectConstants.CLICK);
mOnClickListener.onClick(this);
return true;
}
return false;
}
只要mOnClickListener不是null,就会调用它的onclick方法,赋值在:
public void setOnClickListener(OnClickListener l) {
if (!isClickable()) {
setClickable(true);
}
mOnClickListener = l;
}
总结
1.整个view事件转发流程
view.dispatchTouchEvent -> View.setOnclickListener ->View.onTouchEvent
在dispatchTouchEvent 中进行OnTouchListener判断,如果不为空,则返回true,表示消费掉,不执行ontouchevent。否则为false,执行ontouchevent。