Android 中 OnTouch事件的研究

简介:

在Android中当一个ViewGroup 中包含一个组件,当点击这个组件同时监听其onTouch事件,那么到底是父组件响应还是子组件响应呢?傻蛋在网上找到了一篇相关的帖子,照着例子测试了一番,发现网上的那篇有点问题,先把测试结果总结如下.

 

在触发OnTouch事件的时候Android的GroupView会调用如下三个函数:

public boolean dispatchTouchEvent(MotionEvent ev) 用于事件的分发

public boolean onInterceptTouchEvent(MotionEvent ev) 用于事件的拦截

public boolean onTouchEvent(MotionEvent ev) 处理事件

当然我们可以在容器类中如继承自LinearLayout的类中重写这三个方法。而继承View类的子类只能重写dispatch和onTouchEvent两个方法。当点击后这三个方法相继执行。

 

自己写了一个TextView子类MyTextView和LinearLayout子类MyLinearLayout,TextView包含在LinearLayout中。

当点击MyTextView时,程序会先进入到LinearLayout的dispatchTouchEvent中,这个类必须调用super.dispatchTouchEvent(ev); 否在后面的两个方法无法触发,所以傻蛋发现这个方法根本没有必要重写,因为框架是在super.dispatchTouchEvent(ev);中来调用onInterceptTouchEvent和onTouchEvent方法的,所以手动的设置dispatchTouchEvent的返回值是无效的,除非你不想让框架触发这两个方法。

当执行完dispathTouchEvent后会执行onInterception方法,如果返回为true,这表示MyLinearLayout把这个Touch事件拦截了,就会执行自己的Ontouch方法。如果为false则表示不拦截,此事件会分发到把事件传递给它的子控件MyTextView中。

当事件传递到MyTextView后,会执行dispatchTouchEvent,然后会执行onTouchEvent。挡在MyTextView中的onTouchEvent返回为false的话,当执行完onTouchEvent中的事件后,事件会再分发给MyLinearLaytout,执行LinearLayout的onTouchEvent。

主要代码如下

 

 
 
  1. /**  
  2.  * MyLinearLayout.java  
  3.  * com.androidtest.touch.test  
  4.  *  
  5.  * Function: TODO  
  6.  *  
  7.  *   ver     date           author  
  8.  * ──────────────────────────────────  
  9.  *           2011-5-24      Leon  
  10.  *  
  11.  * Copyright (c) 2011, TNT All Rights Reserved.  
  12.  */  
  13.    
  14. package com.androidtest.touch.test;  
  15.    
  16. import android.content.Context;  
  17. import android.util.AttributeSet;  
  18. import android.util.Log;  
  19. import android.view.MotionEvent;  
  20. import android.widget.LinearLayout;  
  21.    
  22. /**  
  23.  * ClassName:MyLinearLayout Function: TODO ADD FUNCTION Reason: TODO ADD REASON  
  24.  *  
  25.  * @author Leon  
  26.  * @version  
  27.  * @since Ver 1.1  
  28.  * @Date 2011-5-24  
  29.  */  
  30. public class MyLinearLayout extends LinearLayout {  
  31.     private final static String TAG = MyLinearLayout.class.getSimpleName();  
  32.     public MyLinearLayout(Context context, AttributeSet attrs) {  
  33.    
  34.         super(context, attrs);  
  35.         // TODO Auto-generated constructor stub  
  36.         Log.v(TAG , TAG);  
  37.    
  38.     }  
  39.    
  40.     @Override  
  41.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  42.         int action = ev.getAction();  
  43.    
  44.         switch (action) {  
  45.    
  46.         case MotionEvent.ACTION_DOWN:  
  47.    
  48.             Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");  
  49.    
  50.             break;  
  51.    
  52.         case MotionEvent.ACTION_MOVE:  
  53.    
  54.             Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");  
  55.    
  56.             break;  
  57.    
  58.         case MotionEvent.ACTION_UP:  
  59.    
  60.             Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");  
  61.    
  62.             break;  
  63.    
  64.         case MotionEvent.ACTION_CANCEL:  
  65.    
  66.             Log.d(TAG, "dispatchTouchEvent action:ACTION_CANCEL");  
  67.    
  68.             break;  
  69.    
  70.         }  
  71. //          Log.v(TAG ,  "dispatchTouchEvent "+super.dispatchTouchEvent(ev));  
  72.         super.dispatchTouchEvent(ev);  
  73.         Log.v(TAG ,  "dispatchTouchEvent "+ "test.................");  
  74.         return true;  
  75.     }  
  76.    
  77.     @Override  
  78.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  79.    
  80.         int action = ev.getAction();  
  81.    
  82.         switch (action) {  
  83.    
  84.         case MotionEvent.ACTION_DOWN:  
  85.    
  86.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_DOWN");  
  87.    
  88.             break;  
  89.    
  90.         case MotionEvent.ACTION_MOVE:  
  91.    
  92.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE");  
  93.    
  94.             break;  
  95.    
  96.         case MotionEvent.ACTION_UP:  
  97.    
  98.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP");  
  99.    
  100.             break;  
  101.    
  102.         case MotionEvent.ACTION_CANCEL:  
  103.    
  104.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_CANCEL");  
  105.    
  106.             break;  
  107.    
  108.         }  
  109.    
  110.         return false;  
  111.    
  112.     }  
  113.    
  114.     @Override  
  115.     public boolean onTouchEvent(MotionEvent ev) {  
  116.    
  117.         int action = ev.getAction();  
  118.    
  119.         switch (action) {  
  120.    
  121.         case MotionEvent.ACTION_DOWN:  
  122.    
  123.             Log.d(TAG, "---onTouchEvent action:ACTION_DOWN");  
  124.    
  125.             break;  
  126.    
  127.         case MotionEvent.ACTION_MOVE:  
  128.    
  129.             Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");  
  130.    
  131.             break;  
  132.    
  133.         case MotionEvent.ACTION_UP:  
  134.    
  135.             Log.d(TAG, "---onTouchEvent action:ACTION_UP");  
  136.    
  137.             break;  
  138.    
  139.         case MotionEvent.ACTION_CANCEL:  
  140.    
  141.             Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");  
  142.    
  143.             break;  
  144.    
  145.         }  
  146.    
  147.         return true;  
  148.     }  
  149.    
  150. }  
  151.    
  152. /**  
  153.  * MyTestView.java  
  154.  * com.androidtest.touch.test  
  155.  *  
  156.  * Function: TODO  
  157.  *  
  158.  *   ver     date           author  
  159.  * ──────────────────────────────────  
  160.  *           2011-5-24      Leon  
  161.  *  
  162.  * Copyright (c) 2011, TNT All Rights Reserved.  
  163.  */  
  164.    
  165. package com.androidtest.touch.test;  
  166.    
  167. import android.content.Context;  
  168. import android.util.AttributeSet;  
  169. import android.util.Log;  
  170. import android.view.MotionEvent;  
  171. import android.widget.TextView;  
  172.    
  173. /**  
  174.  * ClassName:MyTestView Function: TODO ADD FUNCTION Reason: TODO ADD REASON  
  175.  *  
  176.  * @author Leon  
  177.  * @version  
  178.  * @since Ver 1.1  
  179.  * @Date 2011-5-24  
  180.  */  
  181. public class MyTestView extends TextView {  
  182.    
  183.     public static final String TAG = MyTestView.class.getSimpleName();  
  184.    
  185.     public MyTestView(Context context, AttributeSet attrs) {  
  186.    
  187.         super(context, attrs);  
  188.         // TODO Auto-generated constructor stub  
  189.         Log.v(TAG, TAG);  
  190.    
  191.     }  
  192.    
  193.     @Override  
  194.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  195.         int action = ev.getAction();  
  196.    
  197.         switch (action) {  
  198.    
  199.         case MotionEvent.ACTION_DOWN:  
  200.    
  201.             Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");  
  202.    
  203.             break;  
  204.    
  205.         case MotionEvent.ACTION_MOVE:  
  206.    
  207.             Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");  
  208.    
  209.             break;  
  210.    
  211.         case MotionEvent.ACTION_UP:  
  212.    
  213.             Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");  
  214.    
  215.             break;  
  216.    
  217.         case MotionEvent.ACTION_CANCEL:  
  218.    
  219.             Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");  
  220.    
  221.             break;  
  222.    
  223.         }  
  224.         return super.dispatchTouchEvent(ev);  
  225.     }  
  226.    
  227.     @Override  
  228.     public boolean onTouchEvent(MotionEvent ev) {  
  229.    
  230.         int action = ev.getAction();  
  231.    
  232.         switch (action) {  
  233.    
  234.         case MotionEvent.ACTION_DOWN:  
  235.    
  236.             Log.d(TAG, "---onTouchEvent action:ACTION_DOWN");  
  237.    
  238.             break;  
  239.    
  240.         case MotionEvent.ACTION_MOVE:  
  241.    
  242.             Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");  
  243.    
  244.             break;  
  245.    
  246.         case MotionEvent.ACTION_UP:  
  247.    
  248.             Log.d(TAG, "---onTouchEvent action:ACTION_UP");  
  249.    
  250.             break;  
  251.    
  252.         case MotionEvent.ACTION_CANCEL:  
  253.    
  254.             Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");  
  255.    
  256.             break;  
  257.    
  258.         }  
  259.         return false;  
  260.    
  261.     }  
  262.    
  263. }  
  264. /**  
  265.  * TestTouchEvent.java  
  266.  * com.androidtest.touch.test  
  267.  *  
  268.  * Function: TODO  
  269.  *  
  270.  *   ver     date           author  
  271.  * ──────────────────────────────────  
  272.  *           2011-5-24      Leon  
  273.  *  
  274.  * Copyright (c) 2011, TNT All Rights Reserved.  
  275. */  
  276.    
  277. package com.androidtest.touch.test;  
  278.    
  279. import com.androidtest.R;  
  280.    
  281. import android.app.Activity;  
  282. import android.os.Bundle;   
  283.    
  284. /**  
  285.  * ClassName:TestTouchEvent  
  286.  * Function: TODO ADD FUNCTION  
  287.  * Reason:   TODO ADD REASON  
  288.  *  
  289.  * @author   Leon  
  290.  * @version  
  291.  * @since    Ver 1.1  
  292.  * @Date     2011-5-24  
  293.  */  
  294. public class TestTouchEvent extends Activity{  
  295.    
  296.     @Override  
  297.     protected void onCreate(Bundle savedInstanceState) {  
  298.    
  299.         // TODO Auto-generated method stub  
  300.         super.onCreate(savedInstanceState);  
  301.         this.setContentView(R.layout.test_touch_event);  
  302.    
  303.     }  
  304.    
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <com.androidtest.touch.test.mylinearlayout android:gravity="center" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 
  3.        <com.androidtest.touch.test.mytestview android:background="#FFFFFF" android:id="@+id/tv" android:layout_height="200px" android:layout_width="200px" android:text="leon" android:textcolor="#0000FF" android:textsize="40sp" android:textstyle="bold"> 
  4. </com.androidtest.touch.test.mytestview></com.androidtest.touch.test.mylinearlayout> 

 


本文转自 最牛傻蛋 51CTO博客,原文链接:http://blog.51cto.com/zuiniuwang/718315,如需转载请自行联系原作者

相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发之捕获屏幕的变更事件实战(包括竖屏与横屏切换,回到桌面与切换到任务列表)
Android Studio App开发之捕获屏幕的变更事件实战(包括竖屏与横屏切换,回到桌面与切换到任务列表)
39 0
|
6月前
|
JSON JavaScript 前端开发
Android 缩减、混淆处理和优化应用研究(二)
Android 缩减、混淆处理和优化应用研究(二)
|
6月前
|
IDE Java 编译器
Android 缩减、混淆处理和优化应用研究(一)
Android 缩减、混淆处理和优化应用研究(一)
|
4天前
|
存储 Java Linux
Android系统获取event事件回调等几种实现和原理分析
Android系统获取event事件回调等几种实现和原理分析
25 0
|
4月前
|
小程序 JavaScript 前端开发
微信小程序(十七)小程序监听返回键跳转事件(安卓返回也适用)
onUnload:function(){ wx.redirectTo({ url: '../index/index' }) wx.navigateTo({ url: '../index/index' }) wx.switchTab({ url: '../../member/member' }) }
309 0
|
1月前
|
定位技术 API 数据库
基于Android的在线移动电子导航系统的研究与实现(论文+源码)_kaic
基于Android的在线移动电子导航系统的研究与实现(论文+源码)_kaic
|
4月前
|
XML Java Android开发
Android App事件交互Event之模仿京东App实现下拉刷新功能(附源码 可直接使用)
Android App事件交互Event之模仿京东App实现下拉刷新功能(附源码 可直接使用)
33 0
Android App事件交互Event之模仿京东App实现下拉刷新功能(附源码 可直接使用)
|
4月前
|
XML Java Android开发
Android App事件交互中辨别缩放与旋转手指的讲解与实战(附源码 可直接使用)
Android App事件交互中辨别缩放与旋转手指的讲解与实战(附源码 可直接使用)
39 0
|
4月前
|
XML Java Android开发
Android App事件交互中区分点击和长按动作以及识别手势滑动方向的讲解及实战(附源码 可直接使用)
Android App事件交互中区分点击和长按动作以及识别手势滑动方向的讲解及实战(附源码 可直接使用)
62 0
|
4月前
|
XML Java Android开发
Android App开发触摸事件中手势事件Event的分发流程讲解与实战(附源码 简单易懂)
Android App开发触摸事件中手势事件Event的分发流程讲解与实战(附源码 简单易懂)
43 0