---
title: "android 事件"
author: "jguangyou"
date: "2012-03-27T20:38:41Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2012-03-27T20:38:41Z"
id: 1465402
url: "https://deep-fish.iteye.com/blog/1465402"
---

在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。
 
主要代码如下
 
```java

**
 * MyLinearLayout.java
 * com.androidtest.touch.test
 *
 * Function： TODO
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2011-5-24 		Leon
 *
 * Copyright (c) 2011, TNT All Rights Reserved.
 */
 
package com.androidtest.touch.test;
 
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;
 
/**
 * ClassName:MyLinearLayout Function: TODO ADD FUNCTION Reason: TODO ADD REASON
 *
 * @author Leon
 * @version
 * @since Ver 1.1
 * @Date 2011-5-24
 */
public class MyLinearLayout extends LinearLayout {
	private final static String TAG = MyLinearLayout.class.getSimpleName();
	public MyLinearLayout(Context context, AttributeSet attrs) {
 
		super(context, attrs);
		// TODO Auto-generated constructor stub
		Log.v(TAG , TAG);
 
	}
 
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
 
		switch (action) {
 
		case MotionEvent.ACTION_DOWN:
 
			Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");
 
			break;
 
		case MotionEvent.ACTION_MOVE:
 
			Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");
 
			break;
 
		case MotionEvent.ACTION_UP:
 
			Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");
 
			break;
 
		case MotionEvent.ACTION_CANCEL:
 
			Log.d(TAG, "dispatchTouchEvent action:ACTION_CANCEL");
 
			break;
 
		}
//        	Log.v(TAG ,  "dispatchTouchEvent "+super.dispatchTouchEvent(ev));
		super.dispatchTouchEvent(ev);
		Log.v(TAG ,  "dispatchTouchEvent "+ "test.................");
		return true;
	}
 
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
 
		int action = ev.getAction();
 
		switch (action) {
 
		case MotionEvent.ACTION_DOWN:
 
			Log.d(TAG, "onInterceptTouchEvent action:ACTION_DOWN");
 
			break;
 
		case MotionEvent.ACTION_MOVE:
 
			Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE");
 
			break;
 
		case MotionEvent.ACTION_UP:
 
			Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP");
 
			break;
 
		case MotionEvent.ACTION_CANCEL:
 
			Log.d(TAG, "onInterceptTouchEvent action:ACTION_CANCEL");
 
			break;
 
		}
 
		return false;
 
	}
 
	@Override
    public boolean onTouchEvent(MotionEvent ev) {
 
        int action = ev.getAction();
 
        switch (action) {
 
        case MotionEvent.ACTION_DOWN:
 
            Log.d(TAG, "---onTouchEvent action:ACTION_DOWN");
 
            break;
 
        case MotionEvent.ACTION_MOVE:
 
            Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");
 
            break;
 
        case MotionEvent.ACTION_UP:
 
            Log.d(TAG, "---onTouchEvent action:ACTION_UP");
 
            break;
 
        case MotionEvent.ACTION_CANCEL:
 
            Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");
 
            break;
 
        }
 
        return true;
    }
 
}
 
/**
 * MyTestView.java
 * com.androidtest.touch.test
 *
 * Function： TODO
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2011-5-24 		Leon
 *
 * Copyright (c) 2011, TNT All Rights Reserved.
 */
 
package com.androidtest.touch.test;
 
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;
 
/**
 * ClassName:MyTestView Function: TODO ADD FUNCTION Reason: TODO ADD REASON
 *
 * @author Leon
 * @version
 * @since Ver 1.1
 * @Date 2011-5-24
 */
public class MyTestView extends TextView {
 
	public static final String TAG = MyTestView.class.getSimpleName();
 
	public MyTestView(Context context, AttributeSet attrs) {
 
		super(context, attrs);
		// TODO Auto-generated constructor stub
		Log.v(TAG, TAG);
 
	}
 
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
 
		switch (action) {
 
		case MotionEvent.ACTION_DOWN:
 
			Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");
 
			break;
 
		case MotionEvent.ACTION_MOVE:
 
			Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");
 
			break;
 
		case MotionEvent.ACTION_UP:
 
			Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");
 
			break;
 
		case MotionEvent.ACTION_CANCEL:
 
			Log.d(TAG, "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, "---onTouchEvent action:ACTION_DOWN");
 
			break;
 
		case MotionEvent.ACTION_MOVE:
 
			Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");
 
			break;
 
		case MotionEvent.ACTION_UP:
 
			Log.d(TAG, "---onTouchEvent action:ACTION_UP");
 
			break;
 
		case MotionEvent.ACTION_CANCEL:
 
			Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");
 
			break;
 
		}
		return false;
 
	}
 
}
/**
 * TestTouchEvent.java
 * com.androidtest.touch.test
 *
 * Function： TODO
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2011-5-24 		Leon
 *
 * Copyright (c) 2011, TNT All Rights Reserved.
*/
 
package com.androidtest.touch.test;
 
import com.androidtest.R;
 
import android.app.Activity;
import android.os.Bundle; 
 
/**
 * ClassName:TestTouchEvent
 * Function: TODO ADD FUNCTION
 * Reason:	 TODO ADD REASON
 *
 * @author   Leon
 * @version
 * @since    Ver 1.1
 * @Date	 2011-5-24
 */
public class TestTouchEvent extends Activity{
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
 
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.test_touch_event);
 
	}
 
}



<?xml version="1.0" encoding="utf-8"?>
<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">
       <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">
</com.androidtest.touch.test.mytestview></com.androidtest.touch.test.mylinearlayout

```