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

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.youkuaiyun.com/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没有响应,自然点击事件也就没有。。。

### 回答1: CentOS 7启动httpd服务失败可能有多种原因,以下是一些常见的解决方法: 1. 检查httpd配置文件是否正确:可以使用命令`httpd -t`检查httpd配置文件是否正确,如果有错误,需要修改配置文件。 2. 检查端口是否被占用:可以使用命令`netstat -tlnp`查看端口是否被占用,如果被占用需要释放端口或修改httpd配置文件中的端口号。 3. 检查httpd服务是否安装:可以使用命令`rpm -qa | grep httpd`查看httpd服务是否安装,如果没有安装需要先安装httpd服务。 4. 检查httpd服务是否启动:可以使用命令`systemctl status httpd`查看httpd服务是否启动,如果没有启动需要使用命令`systemctl start httpd`启动httpd服务。 5. 检查SELinux是否开启:如果SELinux开启,可能会导致httpd服务启动失败,需要使用命令`setenforce 0`关闭SELinux,或者修改SELinux策略。 以上是一些常见的解决方法,如果以上方法都无法解决问题,可以查看httpd服务日志文件,找到具体的错误信息,然后根据错误信息进行解决。 ### 回答2: CentOS 7上的httpd服务启动失败可能有多种原因。以下列出了一些常见问题和解决方法: 1. 端口被占用 当httpd试图占用已被其他程序占用的端口时会启动失败。此时可以通过使用`netstat -tunlp`命令检查端口占用情况,然后杀死占用该端口的进程及时释放端口。或者修改httpd的配置文件,将端口修改为未被占用的端口。 2. 配置文件错误 有时httpd服务的配置文件中可能出现错误,例如语法错误或路径错误等等。在启动httpd服务之前,可以使用`apachectl configtest`命令进行检查,如果输出“Syntax OK”,则表示配置文件没有错误。如果出现错误,则需要根据错误提示进行相应修改。 3. 依赖关系问题 如果httpd依赖的其他程序或库缺失,也会导致启动失败。可以通过使用`systemctl status httpd.service`命令来查看httpd服务状态,如果输出“Failed to start”或“Loaded: failed”,则需要检查依赖关系是否完整。 4. SELinux问题 当SELinux启用时,有时会导致httpd服务启动失败。在这种情况下,可以在SELinux上禁用httpd服务,或者修改httpd配置文件解决SELinux相关的问题。 5. 用户权限问题 httpd服务启动可能需要特定的用户权限。如果使用的用户权限不够,则无法启动。可以尝试使用root用户启动httpd服务,或者根据需要修改相应的用户权限。 ### 回答3: CentOS 7中的Apache HTTP服务器(httpd)是一个常见的Web服务器,如果遇到httpd服务启动失败的情况,可能会影响服务器正常的工作和对外服务的稳定性。本文将提供一些可能会导致httpd服务启动失败的原因,并给出相应的解决方法。 1. 端口被占用 如果端口被其他进程占用,httpd服务就无法启动。可以通过 netstat -tulpn 命令查看端口占用情况,并杀死占用该端口的进程。如果端口被 httpd 服务自身占用,可以通过 systemctl restart httpd 命令重启 httpd 服务;如果是其他进程占用了端口,可以通过 kill 命令杀死该进程或更改 httpd.conf 文件配置,将 httpd 服务的端口改为其他空闲端口,重新启动。 2. 配置文件错误 httpd 服务的配置文件通常是 /etc/httpd/conf/httpd.conf,如果其中存在语法错误、权限问题或者其它配置错误,可能会导致 httpd 服务启动出错。可以通过将 httpd.conf 文件备份后删掉,重新执行 yum install httpd 命令安装 httpd 服务,然后手动修改 httpd.conf 文件,逐个检查每个配置项是否正确,确认无误后重启 httpd 服务。 3. SELinux 问题 SELinux 是 CentOS 7中提供的一种安全模块,它可以对系统文件和应用程序进行安全管控。如果 SELinux 配置不正确,可能会阻止 httpd 服务正常启动。可以通过修改 /etc/selinux/config 文件中 SELINUX=disabled 来暂时关闭 SELinux,然后重新启动 httpd 服务;或者一个更优的方式是,根据日志确定问题原因,使用命令 semanage 或者 setsebool 等工具将相关目录或者配置加入到 SELinux 许可列表中,重新启动 httpd 服务,以恢复服务正常工作。 4. 防火墙问题 如果你的 CentOs 7 服务器启用了防火墙,有可能会导致 httpd 服务启动失败。可以通过检查防火墙相关配置来确定问题原因,解决方案是修改防火墙规则,将端口 80 或者 443 等 httpd 服务需要的端口放行,重新启动 httpd 服务。 总之,当遇到 httpd 服务启动失败时,不要慌张,可以先通过日志或者执行命令查看错误信息,找到错误原因,然后根据错误原因一步一步解决问题。在解决问题过程中注意备份原始配置文件,以免造成不必要的损失。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值