1.android 中的事件处理是从activity 开始的,先走的是activity 的dispatchtTouchEvent ,然后走ViewGroup 的disaptchTounchEvent方法,最后的走的view的dispathTouchEvent方法

2.android 中默认的返回值 dispatchTouchEvent 的返回值是True (分发事件) onInterceptTouchEvent 的返回值是false (不拦截事件) view的OnTouch方法的返回值是false 不处理事件

3.TouchEvent 分三种事件:down、move、up。

其中move事件在一个操作中(这里说的一个操作就是用户与屏幕的交互,即由down到up的动作序列)可能会发生多次。
但是,我们认为一个动作序列会包含以上三种事件,因此,在事件处理中就是要处理好这个过程,而最重要的就是down事件,这是一个动作序列的起始,没有down谈不上后面的事件了。
所以,我们把消耗down事件的类当做是这个动作序列的最终载体。

如果Down事件不归你处理,那这个动作序列的move,up也不归你处理。

他们的触发顺序会是这样:

ACTION_DOWN->ACTION_MOVE->ACTION_MOVE->ACTION_MOVE...->ACTION_MOVE->ACTION_UP

我的理解是,在的你View中第一次接受的事件后,你不做处理,以后的事件也不会到你这了。打个比方有个人

第一个找你帮忙,你没帮,那个人以后就在也不找你帮忙了!

package com.example.touch;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class MyLinearLayout extends LinearLayout {
  private final String TAG = "System";

  public MyLinearLayout(Context context, AttributeSet attrs) {

      super(context, attrs);

      Log.d(TAG, TAG);

  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
      int action = ev.getAction();

      switch (action) {

      case MotionEvent.ACTION_DOWN:

          Log.d(TAG, "MyLinearLayout dispatchTouchEvent action:ACTION_DOWN");

          break;

      case MotionEvent.ACTION_MOVE:

          Log.d(TAG, "MyLinearLayoutdispatchTouchEvent action:ACTION_MOVE");

          break;

      case MotionEvent.ACTION_UP:

          Log.d(TAG, "MyLinearLayout dispatchTouchEvent action:ACTION_UP");

          break;

      case MotionEvent.ACTION_CANCEL:

          Log.d(TAG, "MyLinearLayout dispatchTouchEvent action:ACTION_CANCEL");

          break;

      }//super.dispatchTouchEvent(ev);
//      Log.d(TAG, "MyLinearLayout dispatchTouchEvent default="+super.dispatchTouchEvent(ev));
      return super.dispatchTouchEvent(ev);
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {

      int action = ev.getAction();

      switch (action) {

      case MotionEvent.ACTION_DOWN:

          Log.d(TAG, "MyLinearLayout onInterceptTouchEvent action:ACTION_DOWN");

          break;

      case MotionEvent.ACTION_MOVE:

          Log.d(TAG, "MyLinearLayout onInterceptTouchEvent action:ACTION_MOVE");

          break;

      case MotionEvent.ACTION_UP:

          Log.d(TAG, "MyLinearLayout onInterceptTouchEvent action:ACTION_UP");

          break;

      case MotionEvent.ACTION_CANCEL:

          Log.d(TAG, "MyLinearLayout onInterceptTouchEvent action:ACTION_CANCEL");

          break;

      }
      Log.d(TAG, "MyLinearLayout onInterceptTouchEvent default="+super.onInterceptTouchEvent(ev));
      return super.onInterceptTouchEvent(ev);

  }

  @Override
  public boolean onTouchEvent(MotionEvent ev) {

      int action = ev.getAction();

      switch (action) {

      case MotionEvent.ACTION_DOWN:

          Log.d(TAG, "MyLinearLayout---onTouchEvent action:ACTION_DOWN");

          break;

      case MotionEvent.ACTION_MOVE:

          Log.d(TAG, "MyLinearLayout---onTouchEvent action:ACTION_MOVE");

          break;

      case MotionEvent.ACTION_UP:

          Log.d(TAG, "MyLinearLayout---onTouchEvent action:ACTION_UP");

          break;

      case MotionEvent.ACTION_CANCEL:

          Log.d(TAG, "MyLinearLayout---onTouchEvent action:ACTION_CANCEL");

          break;

      }

      return true;
  }

}


package com.example.touch;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

public class MyTextView extends TextView {

  private final String TAG = "System";

  public MyTextView(Context context, AttributeSet attrs) {

      super(context, attrs);

  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
      int action = ev.getAction();

      switch (action) {

      case MotionEvent.ACTION_DOWN:

          Log.d(TAG, "MyTextView dispatchTouchEvent action:ACTION_DOWN");

          break;

      case MotionEvent.ACTION_MOVE:

          Log.d(TAG, "MyTextView dispatchTouchEvent action:ACTION_MOVE");

          break;

      case MotionEvent.ACTION_UP:

          Log.d(TAG, "MyTextView dispatchTouchEvent action:ACTION_UP");

          break;

      case MotionEvent.ACTION_CANCEL:

          Log.d(TAG, "MyTextView onTouchEvent action:ACTION_CANCEL");

          break;

      }
      return super.dispatchTouchEvent(ev);
  }

  @Override
  public boolean onTouchEvent(MotionEvent ev) {

      int action = ev.getAction();

      switch (action) {

      case MotionEvent.ACTION_DOWN:

          Log.d(TAG, "MyTextView ---onTouchEvent action:ACTION_DOWN  "+super.onTouchEvent(ev));

          break;

      case MotionEvent.ACTION_MOVE:

          Log.d(TAG, "MyTextView ---onTouchEvent action:ACTION_MOVE");

          break;

      case MotionEvent.ACTION_UP:

          Log.d(TAG, "MyTextView ---onTouchEvent action:ACTION_UP");

          break;

      case MotionEvent.ACTION_CANCEL:

          Log.d(TAG, "MyTextView ---onTouchEvent action:ACTION_CANCEL");

          break;

      }

      return true;

  }

}




package com.example.touch;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

public class TestTouchEventApp extends Activity {
  /** Called when the activity is first created. */
     private final String TAG = "System";
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  }




<?xml version="1.0" encoding="utf-8"?>
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center" >
               android:layout_width="200px"
          android:layout_height="200px"
          android:id="@+id/tv"
          android:text="lzqdiy"
          android:textSize="40sp"
          android:textStyle="bold"
          android:background="#FFFFFF"
          android:textColor="#0000FF"/>



为了指代方便,下面将MyLinearLayout简称为L,将MyTextView简称为T,

L.onInterceptTouchEvent=true表示的含义为MyLinearLayout中的onInterceptTouchEvent

方法返回值为true,通过程序运行时输出的Log来说明调用时序。

L.onInterceptTouchEvent=true表示的含义为MyLinearLayout中的onInterceptTouchEvent

方法返回值为true,通过程序运行时输出的Log来说明调用时序。
第1种情况L.onInterceptTouchEvent=false&&L.onTouchEvent=true

&&T.onTouchEvent=true输出下面的Log:
D/MyLinearLayout(11865): dispatchTouchEventaction:ACTION_DOWN
D/MyLinearLayout(11865): onInterceptTouchEventaction:ACTION_DOWN
D/MyTextView(11865): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(11865): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(11865): dispatchTouchEventaction:ACTION_MOVE
D/MyLinearLayout(11865): onInterceptTouchEventaction:ACTION_MOVE
D/MyTextView(11865): dispatchTouchEvent action:ACTION_MOVE
D/MyTextView(11865): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(11865): onInterceptTouchEventaction:ACTION_UP
D/MyTextView(11865): dispatchTouchEvent action:ACTION_UP
D/MyTextView(11865): ---onTouchEvent action:ACTION_UP
结论:TouchEvent完全由TextView处理。
第2种情况L.onInterceptTouchEvent=false&&L.onTouchEvent=true

&&T.onTouchEvent=false输出下面的Log:
D/MyLinearLayout(13101): dispatchTouchEventaction:ACTION_DOWN
D/MyLinearLayout(13101): onInterceptTouchEventaction:ACTION_DOWN
D/MyTextView(13101): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(13101): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): dispatchTouchEventaction:ACTION_MOVE
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_UP
结论:TextView只处理了ACTION_DOWN事件,LinearLayout处理了所有的TouchEvent。
第3种情况L.onInterceptTouchEvent=true&&L.onTouchEvent=true 输出下面的Log:
D/MyLinearLayout(13334): dispatchTouchEventaction:ACTION_DOWN
D/MyLinearLayout(13334): onInterceptTouchEventaction:ACTION_DOWN
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): dispatchTouchEventaction:ACTION_MOVE
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_UP
结论:LinearLayout处理了所有的TouchEvent。
第4种情况L.onInterceptTouchEvent=true&&L.onTouchEvent=false 输出下面的Log:
D/MyLinearLayout(13452): dispatchTouchEventaction:ACTION_DOWN
D/MyLinearLayout(13452): onInterceptTouchEventaction:ACTION_DOWN
D/MyLinearLayout(13452): ---onTouchEvent action:ACTION_DOWN
结论:LinearLayout只处理了ACTION_DOWN事件,那么其他的TouchEvent被谁处理了呢?

答案是LinearLayout最外层的Activity处理了TouchEvent。

















更多相关文章

  1. 【Android游戏开发十五】关于Android(安卓)游戏开发中 OnTouchEv
  2. 高通平台android串口没有输出
  3. android:使用WebView, WebChromeClient和WebViewClient加载网页
  4. Android事件分发机制的探索与发现之View篇
  5. Android通话默认打开扬声器
  6. Android(安卓)xml 解析
  7. android post数据到服务器端工具类(包括postjson字符串、键值对)
  8. Android(安卓)传感器(Sensor)API教程 (二) 传感器事件
  9. CheckBox in ListView

随机推荐

  1. android 的事件分发从源码分析
  2. Android studio的常见布局
  3. 性能优化工具
  4. android面试题总结—摘自csdn
  5. Android:布局(线性布局LinearLayout)
  6. Android ActionBar的源代码分析(一)
  7. 学习笔记| (二)IPC机制
  8. android --多线程下载
  9. android中SurfaceView SurfaceHolder Sur
  10. 安卓ListView去除分割线、设置边距