最近因为项目中用到类似一个LinearLayout中水平布局中,有一个TextView和Button,然后对该LinearLayout布局设置点击事件,点击TextView能够触发该点击事件,然而奇怪的是点击Button却不能触发。然后google到了解决办法(重写Button,然后重写其中的ontouchEvent方法,且返回值为false),但是不知道原因,这两天看了几位大神的博客,然后自己总结下。
public class MyButton extends Button { private final static String TAG = "MyButton::zjt"; public MyButton(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MyButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { 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); return false; } @Override public boolean dispatchTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { 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); }}
MyTextView.Java
public class MyTextView extends TextView { private final static String TAG = "MyTextView : "; public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { 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); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "onTouchEvent ACTION_DOWN"); ////return true 后面的ACTION_MOVE、和ACTION_UP能够得以执行,如果不做任何操作,即 break,由于textview默认是不可点击和长点击的,所以return false, //那么 dispatcTouchEvent 会 return false,导致后面的ACTION_MOVE 和 ACTION_UP不能执行 //return true; 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); }}
MainActivity如下:
public class TestTouchActivity extends Activity { private final static String TAG = "TestTouchActivity"; private Button mButton; private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.my_button_layout); mButton = (Button) findViewById(R.id.my_btn); mTextView = (TextView) findViewById(R.id.my_textview);// mTextView.setOnClickListener(new OnClickListener() {// // @Override// public void onClick(View v) {// // TODO Auto-generated method stub// Log.e(TAG, "mTextView onClick");// }// }); mButton.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "onTouch ACTION_DOWN"); return true; //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; } }); mTextView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "mTextView onTouch ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "mTextView onTouch ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "mTextView onTouch ACTION_UP"); break; default: break; } return false; } }); }}
点击Button和TextView的节目如下:
点击TextView:
为什么结果是这样的,参考博文已经写得很精彩了,我就站在巨人的肩膀上,总结下,我们从上面的结果可以看出,当我们点击屏幕上的View的时候首先触发的是View的dispatchTouchEvent事件。源码如下:
/** * Pass the touch screen motion event down to the target view, or this * view if it is the target. * * @param event The motion event to be dispatched. * @return True if the event was handled by the view, false otherwise. */ public boolean dispatchTouchEvent (MotionEvent event) { if (mOnTouchListener != null && ( mViewFlags & ENABLED_MASK) == ENABLED && mOnTouchListener.onTouch( this, event)) { return true; } return onTouchEvent(event); }
上面的mOnTouchListener 就是我们在Activity中设置的Touch事件,我们设置的时候在onTouch中返回的是false,所以会接着执行下面的onTouchEvent方法,可以看出onTouchEvent的返回值就是dispatchTouchEvent 的返回值。onTouchEvent这个方法源码比较长,我截断了。
public boolean onTouchEvent(MotionEvent event) { 。。。。。。。。。。。 此处有省略 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) { switch (event.getAction()) { 。。。。。。。。。。。 此处有省略 } return true; } return false; }
第4行就是判断该View是否是可点击或者可长按的,如果是返回true。在onTouchEvent中先执行ACTION_DOWN(手指按下),如果返回true,那么dispatchTouchEvent 的返回值也就是true,就可以接着执行后面的ACTION_MOVE和ACTION_UP方法。如果返回false,那么后面的ACTION_MOVE和ACTION_UP就不执行了,这个具体原因我还不知道,如果有知道的可以分享下。
说明 1:长按事件是在onTouchEvent中的ACTION_DOWN中触发的(如果你设置了长按事件),而点击onclick事件是在ACTION_UP中触发的。
现在分析下前面的例子:
由于button默认是可点击的,所以在onTouchEvent中会返回true,所以dispatchTouchEvent 也会返回true,后面的ACTION_MOVE和ACTION_UP可以接着执行。
而TextView默认是不可点击的所以onTouchEvent中会返回false,那么dispatchTouchEvent 也会返回false,后面的ACTION_MOVE和ACTION_UP就执行不到了,和上面打印的log相符。
如果我们在Activity中对TextView设置onTouch事件返回true,结果会怎么样呢,我们先就着dispatchTouchEvent 的源码分析下:
mTextView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } });
由于返回true,我们从dispatchTouchEvent 源码的第10行可以看出mOnTouchListener.onTouch( this, event))即返回true,那么if条件就成立了,dispatchTouchEvent 直接返回true,接着执行后面的ACTION_MOVE和ACTION_UP,(ACTION_MOVE如果你点击的时候滑动了才会执行)。但是后面的onTouchEvent就执行不到了。
log如下:
没有执行ACTION_MOVE是因为我快速点击且没有滑动,从log可以看出执行完dispatchTouchEvent 的ACTION_DOWN之后又执行了dispatchTouchEvent 的ACTION_UP。但并没有执行onTouchEvent。
下面开始讲主题了,也就是前言交代的问题。下面是我自定义的ViewGroup:
public class MyLinearLayout extends LinearLayout { private final static String TAG = "MyLinearLayout :"; public MyLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub int action = ev.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(ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "onInterceptTouchEvent , ACTION_DOWN"); //return true; break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "onInterceptTouchEvent , ACTION_MOVE"); //return true; break; case MotionEvent.ACTION_UP: Log.e(TAG, "onInterceptTouchEvent , ACTION_UP"); break; default: break; } return super.onInterceptTouchEvent(ev); //return true; } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "onTouchEvent , ACTION_DOWN"); //return true; 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 void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { // TODO Auto-generated method stub Log.e(TAG, "enter requestDisallowInterceptTouchEvent"); super.requestDisallowInterceptTouchEvent(disallowIntercept); }
xml如下:
<?xml version="1.0" encoding="utf-8"?><com.example.test.view.touch.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/id_my_linearlayout" > <com.example.test.view.touch.MyButton android:id="@+id/btn_click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="click me" /> <com.example.test.view.touch.MyTextView android:id="@+id/my_textview_click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview click" android:textSize="30sp" /></com.example.test.view.touch.MyLinearLayout>
MainActivity如下:
package com.example.test.view.touch;import com.example.drawview.R;import android.app.Activity;import android.os.Bundle;import android.provider.Telephony.Mms;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;public class TestViewGroupeTouchActivity extends Activity { private final static String TAG = "TestViewGroupeTouchActivity : "; private Button mButton ; private TextView mTextView; private LinearLayout mLinearLayout ; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.my_linear_layout); mButton = (Button) findViewById(R.id.btn_click); mTextView = (TextView) findViewById(R.id.my_textview_click); mLinearLayout = (LinearLayout) findViewById(R.id.id_my_linearlayout); mLinearLayout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.e(TAG, "mLinearLayout , onClick"); } }); mLinearLayout.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "mLinearLayout , onTouch ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "mLinearLayout , onTouch ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "mLinearLayout ,onTouch ACTION_UP"); break; default: break; } return false; } }); mButton.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "mButton onTouch ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "mButton onTouch ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "mButton onTouch ACTION_UP"); break; default: break; } return false; } }); mTextView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.e(TAG, "mTextView , onTouch ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.e(TAG, "mTextView , onTouch ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.e(TAG, "mTextView , onTouch ACTION_UP"); break; default: break; } return false; } }); }}
说明2: 由于我是为了说明前言里面的问题,所以ViewGroup的touch事件分发,我不作过多的说明。ViewGroup事件分发的流程是:dispatchTouchEvent >onInterceptTouchEvent―>然后到手指点击View的事件分发(参考上面所说的View的事件分发)。
onInterceptTouchEvent默认返回false,表示是否拦截事件。ViewGroup的dispatchTouchEvent的源码如下:
/** * {@inheritDoc} */ @Override public boolean dispatchTouchEvent(MotionEvent ev) { final int action = ev.getAction(); final float xf = ev.getX(); final float yf = ev.getY(); final float scrolledXFloat = xf + mScrollX; final float scrolledYFloat = yf + mScrollY; final Rect frame = mTempRect; //这个值默认是false, 然后我们可以通过requestDisallowInterceptTouchEvent(boolean disallowIntercept)方法 //来改变disallowIntercept的值 boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; //这里是ACTION_DOWN的处理逻辑 if (action == MotionEvent.ACTION_DOWN) { //清除mMotionTarget, 每次ACTION_DOWN都很设置mMotionTarget为null if (mMotionTarget != null) { mMotionTarget = null; } //disallowIntercept默认是false, 就看ViewGroup的onInterceptTouchEvent()方法 if (disallowIntercept || !onInterceptTouchEvent(ev)) { ev.setAction(MotionEvent.ACTION_DOWN); final int scrolledXInt = (int) scrolledXFloat; final int scrolledYInt = (int) scrolledYFloat; final View[] children = mChildren; final int count = mChildrenCount; //遍历其子View for (int i = count - 1; i >= 0; i--) { final View child = children[i]; //如果该子View是VISIBLE或者该子View正在执行动画, 表示该View才 //可以接受到Touch事件 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { //获取子View的位置范围 child.getHitRect(frame); //如Touch到屏幕上的点在该子View上面 if (frame.contains(scrolledXInt, scrolledYInt)) { // offset the event to the view's coordinate system final float xc = scrolledXFloat - child.mLeft; final float yc = scrolledYFloat - child.mTop; ev.setLocation(xc, yc); child.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; //调用该子View的dispatchTouchEvent()方法 if (child.dispatchTouchEvent(ev)) { // 如果child.dispatchTouchEvent(ev)返回true表示 //该事件被消费了,设置mMotionTarget为该子View mMotionTarget = child; //直接返回true return true; } // The event didn't get handled, try the next view. // Don't reset the event's location, it's not // necessary here. } } } } } //判断是否为ACTION_UP或者ACTION_CANCEL boolean isUpOrCancel = (action == MotionEvent.ACTION_UP) || (action == MotionEvent.ACTION_CANCEL); if (isUpOrCancel) { //如果是ACTION_UP或者ACTION_CANCEL, 将disallowIntercept设置为默认的false //假如我们调用了requestDisallowInterceptTouchEvent()方法来设置disallowIntercept为true //当我们抬起手指或者取消Touch事件的时候要将disallowIntercept重置为false //所以说上面的disallowIntercept默认在我们每次ACTION_DOWN的时候都是false mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; } // The event wasn't an ACTION_DOWN, dispatch it to our target if // we have one. final View target = mMotionTarget; //mMotionTarget为null意味着没有找到消费Touch事件的View, 所以我们需要调用ViewGroup父类的 //dispatchTouchEvent()方法,也就是View的dispatchTouchEvent()方法 if (target == null) { // We don't have a target, this means we're handling the // event as a regular view. ev.setLocation(xf, yf); if ((mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { ev.setAction(MotionEvent.ACTION_CANCEL); mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; } return super.dispatchTouchEvent(ev); } //这个if里面的代码ACTION_DOWN不会执行,只有ACTION_MOVE //ACTION_UP才会走到这里, 假如在ACTION_MOVE或者ACTION_UP拦截的 //Touch事件, 将ACTION_CANCEL派发给target,然后直接返回true //表示消费了此Touch事件 if (!disallowIntercept && onInterceptTouchEvent(ev)) { final float xc = scrolledXFloat - (float) target.mLeft; final float yc = scrolledYFloat - (float) target.mTop; mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; ev.setAction(MotionEvent.ACTION_CANCEL); ev.setLocation(xc, yc); if (!target.dispatchTouchEvent(ev)) { } // clear the target mMotionTarget = null; // Don't dispatch this event to our own view, because we already // saw it when intercepting; we just want to give the following // event to the normal onTouchEvent(). return true; } if (isUpOrCancel) { mMotionTarget = null; } // finally offset the event to the target's coordinate system and // dispatch the event. final float xc = scrolledXFloat - (float) target.mLeft; final float yc = scrolledYFloat - (float) target.mTop; ev.setLocation(xc, yc); if ((target.mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { ev.setAction(MotionEvent.ACTION_CANCEL); target.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; mMotionTarget = null; } //如果没有拦截ACTION_MOVE, ACTION_DOWN的话,直接将Touch事件派发给target return target.dispatchTouchEvent(ev); }
当我们点击button的时候,由于我们Activity中重写的onInterceptTouchEvent返回值为super.onInterceptTouchEvent(ev);即默认的false,那么25行的if条件!onInterceptTouchEvent(ev))为true。进入if语句里面,遍历所有的子View,然后执行51行的if (child.dispatchTouchEvent(ev)),上面讲到了Button是可点击的,那么MyButton的onTouchEvent返回值为true,即dispatchTouchEvent返回值为true。消费了该事件,所有不会触发mLinearLayout的点击事件。log如下:
那么问题来了,为什么将MyButton 的onTouchEvent返回值设为false,然后点击Button就会触发mLinearLayout的点击事件呢?
我们来分析下:将MyButton 的onTouchEvent返回值设为false,那么51行的if (child.dispatchTouchEvent(ev))的返回值为false,为什么呢?
上面分析View的dispatchTouchEvent源码时分析过了。 返回了false,那么看ViewGroup的源码,81行, final View target = mMotionTarget; 由于51行的if (child.dispatchTouchEvent(ev))返回false,所以没有对mMotionTarget进行赋值, mMotionTarget == null。
所以走到85行:if (target == null) //target = mMotionTarget ,所以该if条件成立。
走到93行:return super.dispatchTouchEvent(ev);
执行第9行的super.dispatchTouchEvent(ev),viewgroup的Super是View,即执行View的dispatchTouchEvent方法。由于我们在Activity中47行设置了ontouch事件,所以先执行Activity 中 mLinearLayout.setOnTouchListener中的onTouch, onTouch返回false ,接着执行 MyLinearLayout 中的onTouchEvent。
说明:
本来由于MyLinearLayout 是继承自LinearLayout,默认和textview一样是没有点击(clickable)或长按(longclickable)的能力的。但是,我们在Activity的38行对他设置了点击事件,mLinearLayout.setOnClickListener,所以MyLinearLayout 获得 点击的能力。
所以MyLinearLayout的onTouchEvent返回true,然后执行MyLinearLayout的onTouchEvent的ACTION_UP,而点击事件就是在ACTION_UP中执行的(说明1)。所有触发了mLinearLayout.setOnClickListener点击事件。log 如下:
总结:
1.Touch事件是从顶层的View一直往下分发到手指按下的最里面的View,如果这个View的onTouchEvent()返回false,即不消费Touch事件,这个Touch事件就会向上找父布局调用其父布局的onTouchEvent()处理,如果这个View返回true,表示消费了Touch事件,就不调用父布局的onTouchEvent()。
2.一个clickable或者longClickable的View会永远消费Touch事件,不管他是enabled还是disabled的。
3.View的长按事件是在ACTION_DOWN中执行,要想执行长按事件该View必须是longClickable的,如果设置的长按事件中返回true,那么clickable事件不会触发。并且不能产生ACTION_MOVE。
4.View的点击事件是在ACTION_UP中执行,想要执行点击事件的前提是消费了ACTION_DOWN和ACTION_MOVE,并且是在没有设置OnLongClickListener的情况下,如设置了OnLongClickListener的情况,则必须使onLongClick()返回false。
5.如果View设置了onTouchListener了,并且onTouch()方法返回true,则不执行View的onTouchEvent()方法,也表示View消费了Touch事件,返回false则继续执行onTouchEvent()。
6.Touch事件是从最顶层的View一直分发到手指touch的最里层的View,如果最里层View消费了ACTION_DOWN事件(设置onTouchListener,并且onTouch()返回true 或者onTouchEvent()方法返回true)才会触发ACTION_MOVE,ACTION_UP的发生,如果某个ViewGroup拦截了Touch事件,则Touch事件交给ViewGroup处理。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。
新闻热点
疑难解答