首先我门先说一下View事件的传递的机制,首先我们先自定义一个类MyView继承自View 然后在MyView中复写父类的两个方法dispatchTouchEvent(MotionEvent event)和onTouchEvent(MotionEvent event)方法 然后在进行实验,我先说说我的实验的结果是 如果是继承自View类的话,事件的主入口就是dispatchTouchEvent(MotionEvent event)(其实ViewGroup中也有dispatchTouchEvent(MotionEvent event)这个方法,千万不要以为和View中的一样,当你查看的源码的时候它们是不用的方法),事件是通过dispatchTouchEvent(MotionEvent event)方法中的返回结果确定事件是否进行传递,如果返回true则代表事件被消费,如果返回false这表示不进行拦截,系统默认的是返回false 。
查看dispatch方法的系统的源码
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 (li != null && li.mOnTouchListener != null&& (mViewFlags & ENABLED_MASK) == ENABLED && li.mOnTouchListener.onTouch(this, event))这句话中只有mOnTouchListener.onTouch(this, event)默认返回的是false所以默认是事件是不会拦截的,只有四个变量都是真的时候才返回true,
最后再进行翻看的的时候发现mOnTouchListener.onTouch(this, event)中调用一个PerformClick这个类。在类中用了onclick();响应了按钮的点击的事件
下面是我自己的测试的代码
<RelativeLayout 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"
>
<com.chaoli_chen.ontouchdemo.Mybutton
android:onClick="bt_click"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="实验按钮"
/>
</RelativeLayout>
自定义一个button复写其中的dispatchTouchEvent(MotionEvent event)和onTouchEvent(MotionEvent event)
package com.chaoli_chen.ontouchdemo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
public class Mybutton extends Button {
public Mybutton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public Mybutton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Mybutton(Context context) {
super(context);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(".button.dispatchTouchEvent.ACTION_DOWN...");
break;
case MotionEvent.ACTION_MOVE:
System.out.println(".button.dispatchTouchEvent.ACTION_MOVE...");
break;
case MotionEvent.ACTION_UP:
System.out.println(".button.dispatchTouchEvent.ACTION_UP...");
break;
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(".button.onTouchEvent.ACTION_DOWN...");
break;
case MotionEvent.ACTION_MOVE:
System.out.println(".button.onTouchEvent.ACTION_MOVE...");
break;
case MotionEvent.ACTION_UP:
System.out.println(".button.onTouchEvent.ACTION_UP...");
break;
}
return super.onTouchEvent(event);
}
}
在MainActivity中同样的复写两个方法dispatchTouchEvent(MotionEvent event)和 onTouchEvent(MotionEvent event)还有给button按钮设置一个点击事件的监听setOnTouchListener(i)
package com.chaoli_chen.ontouchdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.bt);
bt.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(".bt..TouchEvent.ACTION_DOWN...");
break;
case MotionEvent.ACTION_MOVE:
System.out.println(".bt..TouchEvent.ACTION_MOVE...");
break;
case MotionEvent.ACTION_UP:
System.out.println(".bt..TouchEvent.ACTION_UP...");
break;
}
return false;
}
});
}
public void bt_click(View view) {
System.out.println("...bt_click...");
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(".main.dispatchTouchEvent.ACTION_DOWN...");
break;
case MotionEvent.ACTION_MOVE:
System.out.println(".main.dispatchTouchEvent.ACTION_MOVE...");
break;
case MotionEvent.ACTION_UP:
System.out.println(".main.dispatchTouchEvent.ACTION_UP...");
break;
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(".main.onTouchEvent.ACTION_DOWN...");
break;
case MotionEvent.ACTION_MOVE:
System.out.println(".main.onTouchEvent.ACTION_MOVE...");
break;
case MotionEvent.ACTION_UP:
System.out.println(".main.onTouchEvent.ACTION_UP...");
break;
}
return super.onTouchEvent(event);
}
}
打印的结果如下图
产看日志我门能购知道 程序的主入口是 Activity的dispatchTouchEvent方法,然后传递给我们的button按钮的dispatchTouchEvent方法 再传递给我们的button的onTouch()方法,然后就是onTouchEvent方法。最后才是我门的button相应点击事件的方法。
通过分析上面的结构我们可以对自定义按钮的点击事件进行拦截 ,不让其进行onclick的方法执行,只需要在onTouch()中返回true就可以。
bt.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(".bt..TouchEvent.ACTION_DOWN...");
break;
case MotionEvent.ACTION_MOVE:
System.out.println(".bt..TouchEvent.ACTION_MOVE...");
break;
case MotionEvent.ACTION_UP:
System.out.println(".bt..TouchEvent.ACTION_UP...");
break;
}
return true;
}
});
测试结果如下
通过结果分析我们是可以拦截button的点击的onclick事件。