读书人

android 事件处理历程

发布时间: 2012-07-01 13:15:00 作者: rapoo

android 事件处理过程

转自:http://blog.sina.com.cn/s/blog_81d2da550100w48d.html

import android.app.Activity;
import android.os.Bundle;

public class TestTouchEventApp extends Activity {
?<wbr>?<wbr>?<wbr><br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>@Override<br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>public void onCreate(Bundle savedInstanceState) {<br>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>super.onCreate(savedInstanceState);<br>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>setContentView(R.layout.main);<br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}<br>}<br>package test.lzqdiy;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

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 {
?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>private final String TAG = "MyLinearLayout";</wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>public MyLinearLayout(Context context, AttributeSet attrs) {</wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>super(context, attrs);</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, TAG);</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}</wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>@Override<br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>public boolean dispatchTouchEvent(MotionEvent ev) {<br>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>int action = ev.getAction();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>switch (action) {</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_DOWN:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_MOVE:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_UP:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_CANCEL:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "dispatchTouchEvent action:ACTION_CANCEL");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}<br>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>return super.dispatchTouchEvent(ev);<br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>@Override<br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>public boolean onInterceptTouchEvent(MotionEvent ev) {</wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>int action = ev.getAction();</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>switch (action) {</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_DOWN:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "onInterceptTouchEvent action:ACTION_DOWN");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_MOVE:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_UP:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_CANCEL:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "onInterceptTouchEvent action:ACTION_CANCEL");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>return false;</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}</wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>@Override<br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>public boolean onTouchEvent(MotionEvent ev) {</wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>int action = ev.getAction();</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>switch (action) {</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_DOWN:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "---onTouchEvent action:ACTION_DOWN");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_MOVE:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_UP:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "---onTouchEvent action:ACTION_UP");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_CANCEL:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>return true;<br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

}

package test.lzqdiy;

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 {

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>private final String TAG = "MyTextView";</wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>public MyTextView(Context context, AttributeSet attrs) {</wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>super(context, attrs);</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}</wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>@Override<br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>public boolean dispatchTouchEvent(MotionEvent ev) {<br>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>int action = ev.getAction();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>switch (action) {</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_DOWN:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_MOVE:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_UP:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_CANCEL:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}<br>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>return super.dispatchTouchEvent(ev);<br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>@Override<br>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>public boolean onTouchEvent(MotionEvent ev) {</wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>int action = ev.getAction();</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>switch (action) {</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_DOWN:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "---onTouchEvent action:ACTION_DOWN");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_MOVE:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_UP:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "---onTouchEvent action:ACTION_UP");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>case MotionEvent.ACTION_CANCEL:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>return true;</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

?<wbr>?<wbr>?<wbr><span class="Apple-converted-space">?</span>}</wbr></wbr></wbr>

}

为了指代方便,下面将MyLinearLayout简称为L,将MyTextView简称为T,L.onInterceptTouchEvent=true 表示的含义为MyLinearLayout中的onInterceptTouchEvent方法返回值为true,通过程序运行时输出的Log来说明调用时序。
第1种情况 L.onInterceptTouchEvent=false&& L.onTouchEvent=true &&T.onTouchEvent=true 输出下面的Log:
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_DOWN
D/MyTextView(11865): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(11865): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_MOVE
D/MyLinearLayout(11865): onInterceptTouchEvent action: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): onInterceptTouchEvent action:ACTION_UP
D/MyTextView(11865): dispatchTouchEvent action:ACTION_UP
D/MyTextView(11865): ---onTouchEvent action:ACTION_UP
结论:TouchEvent完全由TextView处理。
第2种情况?<wbr><span class="Apple-converted-space">?</span>L.onInterceptTouchEvent=false&& L.onTouchEvent=true &&T.onTouchEvent=false 输出下面的Log:<br>D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_DOWN<br>D/MyLinearLayout(13101): onInterceptTouchEvent action:ACTION_DOWN<br>D/MyTextView(13101): dispatchTouchEvent action:ACTION_DOWN<br>D/MyTextView(13101): ---onTouchEvent action:ACTION_DOWN<br>D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_DOWN<br>D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_MOVE<br>D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_MOVE<br>...........省略其他的ACTION_MOVE事件Log<br>D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_UP<br>D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_UP<br>结论:TextView只处理了ACTION_DOWN事件,LinearLayout处理了所有的TouchEvent。<br>第3种情况?<wbr><span class="Apple-converted-space">?</span>L.onInterceptTouchEvent=true&& L.onTouchEvent=true 输出下面的Log:<br>D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_DOWN<br>D/MyLinearLayout(13334): onInterceptTouchEvent action:ACTION_DOWN<br>D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_DOWN<br>D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_MOVE<br>D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_MOVE<br>...........省略其他的ACTION_MOVE事件Log<br>D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_UP<br>D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_UP<br>结论:LinearLayout处理了所有的TouchEvent。<br>第4种情况?<wbr><span class="Apple-converted-space">?</span>L.onInterceptTouchEvent=true&& L.onTouchEvent=false 输出下面的Log:<br>D/MyLinearLayout(13452): dispatchTouchEvent action:ACTION_DOWN<br>D/MyLinearLayout(13452): onInterceptTouchEvent action:ACTION_DOWN<br>D/MyLinearLayout(13452): ---onTouchEvent action:ACTION_DOWN<br>结论:LinearLayout只处理了ACTION_DOWN事件,那么其他的TouchEvent被谁处理了呢?答案是LinearLayout最外层的Activity处理了TouchEvent。</wbr></wbr></wbr>

读书人网 >Android

热点推荐