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.
下面看打印。
![這裏寫圖片描述](https://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沒有響應,自然點擊事件也就沒有。。。