这是运行结果和总结:(代码详情见附件)
--------------------------------------------------------------------------------
09-04 10:38:20.124: D/TAG(29590): onTouch execute, action 触摸事件返回为假0
09-04 10:38:20.140: D/TAG(29590): onTouch execute, action 触摸事件返回为假2
09-04 10:38:20.158: D/TAG(29590): onTouch execute, action 触摸事件返回为假2
09-04 10:38:20.176: D/TAG(29590): onTouch execute, action 触摸事件返回为假2
09-04 10:38:20.195: D/TAG(29590): onTouch execute, action 触摸事件返回为假2
09-04 10:38:20.213: D/TAG(29590): onTouch execute, action 触摸事件返回为假2
09-04 10:38:20.232: D/TAG(29590): onTouch execute, action 触摸事件返回为假2
09-04 10:38:20.247: D/TAG(29590): onTouch execute, action 触摸事件返回为假2
09-04 10:38:20.247: D/TAG(29590): onTouch execute, action 触摸事件返回为假1
09-04 10:38:20.248: D/TAG(29590): onClick execute点击事件
--------------------------------------------------------------------------------
09-04 10:40:43.722: D/TAG(30998): onTouch execute, action 触摸事件返回为真0
09-04 10:40:43.744: D/TAG(30998): onTouch execute, action 触摸事件返回为真2
09-04 10:40:43.763: D/TAG(30998): onTouch execute, action 触摸事件返回为真2
09-04 10:40:43.775: D/TAG(30998): onTouch execute, action 触摸事件返回为真2
09-04 10:40:43.776: D/TAG(30998): onTouch execute, action 触摸事件返回为真1
--------------------------------------------------------------------------------
总结:
当触摸事件返回是假的时候,优先执行触摸事件,然后执行点击事件
当触摸事件返回是真的时候,执行触摸事件,不执行点击事件
如果 return true,事件会分发给当前 View并由dispatchTouchEvent方法进行消费,同时事件会停止向下传递;
如果 return false,事件分发分为两种情况:
1.如果当前View获取的事件直接来自 Activity,则会将事件返回给 Activity的onTouchEvent 进行消费;
2.如果当前View获取的事件来自外层父控件,则会将事件返回给父View的onTouchEvent 进行消费。
如果返回系统默认的super.dispatchTouchEvent(ev),事件会自动的分发给当前View的onInterceptTouchEvent方法。
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("TAG", "onClick execute"+"点击事件");
}
});
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("TAG", "onTouch execute, action "+"触摸事件返回为真" + event.getAction());
return true;
}
});
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("TAG", "onTouch execute, action "+"触摸事件返回为假" + event.getAction());
return false;
}
});