您的位置:首页 > 移动开发 > Android开发

我们手指触碰屏幕都做了什么?

2016-07-18 21:02 417 查看
1:首先我自定义一个LinearLayout,和TextView,重写它们的onInterceptTouchEvent和onTouchEvent方法,观察它们都返回值。

前者onInterceptTouchEvent是在ViewGroup里面定义的,android里的布局类一般都会继承此类。

onInterceptTouchEvent是用于拦截手势事件的,每个手势事件都会先调用onInterceptTouchEvent。

onTouchEvent同样也是在view中定义的一个方法。处理传递到view 的手势事件。手势事件类型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL等事件。

其实在Layout中onInterceptTouchEvent默认返回值事false,这样就能将touch事件传递到view控件。先调用ACTION_DOWN事件,当onTouchevent里返回值是true的时候,onTouchevent回继续调用ACTION_UP事件,如果onTouchevent里返回值是false,那么onTouch只会调用ACTION_DOWN而不调用ACTION_UP.

下面看打印。

![这里写图片描述](http://img.blog.csdn.net/20160718200737426)


从这里我们看出,系统默认的Myview的OntouhchEvent返回false,只消费了actiond_down事件,这样后面的action_up事件并没有消费。

当我们把Myview的OntoucheEvent返回true时,当我们按下去再松手我们会发现两个事件,action_down,action_up都被消费了



而且当我们移动的时候,action_move也都被消费了

07-18 20:14:09.862 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:09.862 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_DOWN...false
07-18 20:14:09.862 26916-26916/com.example.zew.demo2 V/ViewRootImpl: finishMotionEvent: handled = true stage=10: View Post IME stage eventTime = 39775261 title= com.example.zew.demo2/com.example.zew.demo2.MainActivity
07-18 20:14:10.515 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.516 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.532 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.532 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.549 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.549 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.566 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.566 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.584 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.585 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.600 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.601 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.616 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.617 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.633 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.634 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.650 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.651 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.667 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.667 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.818 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.818 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.825 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.825 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:14:10.825 26916-26916/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:14:10.825 26916-26916/com.example.zew.demo2 E/tag: Myview....ACTION_UP....false


另外我实验了当action_move返回false的时候,后面的action_move.action_up都是可以响应的,



07-18 20:17:20.961 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:20.961 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_DOWN...false
07-18 20:17:20.962 30633-30633/com.example.zew.demo2 V/ViewRootImpl: finishMotionEvent: handled = true stage=10: View Post IME stage eventTime = 39966360 title= com.example.zew.demo2/com.example.zew.demo2.MainActivity
07-18 20:17:21.617 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.617 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.634 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.634 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.650 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.650 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.667 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.667 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.684 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.684 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.701 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.701 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.717 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.718 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.734 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.734 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.751 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.751 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.768 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.768 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.784 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.784 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.801 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.801 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.818 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.818 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.884 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.884 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....false
07-18 20:17:21.884 30633-30633/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:17:21.884 30633-30633/com.example.zew.demo2 E/tag: Myview....ACTION_UP....false


所以我们的action_down是否消费会直接影响后面,action_move,action_up。

下面我们通过看源码可以得知,dispatcherTouchevent里 面通过onTonchListener.ontouch(ev)返回的值决定view的onTonch能否被执行,假如返回false,那么会走onToucheEvent,假如返回true的话不会走onTouchEvent

下面实验,返回true的情况

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (Mylinearlayout) findViewById(R.id.ll);
mv = (Myview) findViewById(R.id.mv);
mv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

return true;
}
});

}
打印结果


07-18 20:27:22.500 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:22.500 5710-5710/com.example.zew.demo2 V/ViewRootImpl: finishMotionEvent: handled = true stage=10: View Post IME stage eventTime = 40567898 title= com.example.zew.demo2/com.example.zew.demo2.MainActivity

07-18 20:27:23.184 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.201 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.218 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.234 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.251 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.268 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.285 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.301 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.318 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.335 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.354 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.371 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.388 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.405 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.421 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.438 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.455 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.472 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.489 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.505 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.522 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.539 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.640 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.656 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.672 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false

07-18 20:27:23.673 5710-5710/com.example.zew.demo2 E/tag: Mylinearlayout…..onInterceptTouchEvent….false



可以看出并没有走onTouchEvent。

当我们的onToucheListener.ontouch(ev)返回false以后,就会走onTouchEvent。假如这时候我们的OntouchEvent,的action_up返回true,则消费事件,后面的action_move,up都会执行。看log

07-18 20:48:02.283 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.283 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.283 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_DOWN...true
07-18 20:48:02.283 16548-16548/com.example.zew.demo2 V/ViewRootImpl: finishMotionEvent: handled = true stage=10: View Post IME stage eventTime = 41807682 title= com.example.zew.demo2/com.example.zew.demo2.MainActivity
07-18 20:48:02.568 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.568 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.568 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....true
07-18 20:48:02.584 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.584 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.584 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....true
07-18 20:48:02.601 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.601 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.601 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....true
07-18 20:48:02.618 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.618 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.618 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....true
07-18 20:48:02.635 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.635 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.635 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....true
07-18 20:48:02.651 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.651 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.651 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....true
07-18 20:48:02.668 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.668 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.668 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....true
07-18 20:48:02.685 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.685 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.685 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....true
07-18 20:48:02.692 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.692 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.692 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_MOVE....true
07-18 20:48:02.693 16548-16548/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:48:02.693 16548-16548/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:48:02.693 16548-16548/com.example.zew.demo2 E/tag: Myview....ACTION_UP....true
07-18 20:48:02.693 16548-16548/com.example.zew.demo2 E/tag: Myview.....onClick....


注意最后才执行点击事件,onclick.这是为什么呢?

通过看源码我们得知:

public boolean onTouchEvent(MotionEvent event) {

if (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
// take focus if we don't have it already and we should in
// touch mode.
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}

if (prepressed) {
// The button is being released before we actually
// showed it as pressed.  Make it show the pressed
// state now (before scheduling the click) to ensure
// the user sees it.
setPressed(true, x, y);
}

if (!mHasPerformedLongPress) {
// This is a tap, so remove the longpress check
removeLongPressCallback();

// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
}
}

if (mUnsetPressedState == null) {
mUnsetPressedState = new UnsetPressedState();
}

if (prepressed) {
postDelayed(mUnsetPressedState,
ViewConfiguration.getPressedStateDuration());
} else if (!post(mUnsetPressedState)) {
// If the post failed, unpress right now
mUnsetPressedState.run();
}

removeTapCallback();
}
break;
return true;
}

return false;
}


再看看这个方法里performClick()里面做了什么?

public boolean performClick() {
final boolean result;
final ListenerInfo li = mListenerInfo;
if (li != null && li.mOnClickListener != null) {
playSoundEffect(SoundEffectConstants.CLICK);
li.mOnClickListener.onClick(this);
result = true;
} else {
result = false;
}

sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
return result;
}


注意看那个performClick()方法,这个方法是在action_up手势后面执行的。所以最后在action_up响应后会响应点击事件。

看见没??第6行 li.mOnClickListener.onClick(this); 这个接口回调就是我们Button的 onClick事件。到此为止,我们从源码分析了Button事件分发过程

结论:dispatchTouchEvent—->onTouch—->onTouchEvent—–>onClick。并且如果仔细的你会发现,是在所有ACTION_UP事件之后才触发onClick点击事件。

那当我们ontouchlistener.ontouch()返回true的话会怎么样?

07-18 20:54:17.555 22194-22194/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:54:17.555 22194-22194/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:54:17.555 22194-22194/com.example.zew.demo2 V/ViewRootImpl: finishMotionEvent: handled = true stage=10: View Post IME stage eventTime = 42182953 title= com.example.zew.demo2/com.example.zew.demo2.MainActivity
07-18 20:54:17.779 22194-22194/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:54:17.779 22194-22194/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:54:17.796 22194-22194/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:54:17.796 22194-22194/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:54:17.813 22194-22194/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:54:17.813 22194-22194/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:54:17.830 22194-22194/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:54:17.830 22194-22194/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:54:17.846 22194-22194/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:54:17.846 22194-22194/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:54:17.863 22194-22194/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:54:17.863 22194-22194/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:54:17.880 22194-22194/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:54:17.880 22194-22194/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:54:17.888 22194-22194/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:54:17.889 22194-22194/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:54:17.889 22194-22194/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:54:17.889 22194-22194/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down


可以看出没有走onTouchEvent方法自然也没有o’clock点击事件。

那假如让我们ontouchlistener.ontouch()返回false,但是让action_up返回false,会不会响应点击事件。

修改一下` public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()){

case MotionEvent.ACTION_DOWN:

Log.e("tag","Myview....ACTION_DOWN..."+super.onTouchEvent(event));
return false;


// break;

case MotionEvent.ACTION_MOVE:

Log.e(“tag”,”Myview….ACTION_MOVE….”+super.onTouchEvent(event));

break;

case MotionEvent.ACTION_UP:

Log.e(“tag”,”Myview….ACTION_UP….”+super.onTouchEvent(event));

break;

case MotionEvent.ACTION_CANCEL:

Log.e(“tag”,”Myview….ACTION_CANCEL….”+super.onTouchEvent(event));

break;

}
return true;
}


07-18 20:58:16.075 23882-23882/com.example.zew.demo2 E/tag: Mylinearlayout.....onInterceptTouchEvent....false
07-18 20:58:16.075 23882-23882/com.example.zew.demo2 E/tag: Myview.....ontouch....action_down
07-18 20:58:16.075 23882-23882/com.example.zew.demo2 E/tag: Myview....ACTION_DOWN...true
07-18 20:58:16.075 23882-23882/com.example.zew.demo2 E/tag: Mylinearlayout.....onTouchEvent....false


从log,可以看出只响应了我们的action_down事件。后面的action_up没有响应,自然点击事件也就没有。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息