分享

Activity 的 dispatchTouchEvent

 小样样样样样样 2022-11-01 发布于北京
   /**
     * Called to process touch screen events.  You can override this to
     * intercept all touch screen events before they are dispatched to the
     * window.  Be sure to call this implementation for touch screen events
     * that should be handled normally.
     *
     * @param ev The touch screen event.
     *
     * @return boolean Return true if this event was consumed.
     */

   // Activity 对于callBack 接口方法的实现
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // down 事件 回调onUserInteraction() ,空方法
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            onUserInteraction();
        }
        // getWindoe() 返回的就是PhoneWindow 实例
        // 直接调用PhoneWindow的方法
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        // 如果前面分发过程中国呢的事件没有被处理,那么调用Activity自身的方法对事件进行处理
        return onTouchEvent(ev); 
    }



    /**
     * Called when a touch screen event was not handled by any of the views
     * under it.  This is most useful to process touch events that happen
     * outside of your window bounds, where there is no view to receive it.
     *
     * @param event The touch screen event being processed.
     *
     * @return Return true if you have consumed the event, false if you haven't.
     * The default implementation always returns false.
     */
    public boolean onTouchEvent(MotionEvent event) {
        if (mWindow.shouldCloseOnTouch(this, event)) {
            finish();
            return true;
        }

        return false;
    }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多