解決鼠標點擊事件不執行的問題

當button 的屬性被設置focusableInTouchMode 爲true的時候 鼠標的點擊事件就不執行.去掉這個屬性即可

原因:在View的OnTouchEvent 中的action_up事件中有如下邏輯

    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 && !mIgnoreNextUpEvent) {
        // 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)) {
                performClickInternal();
            }
        }
    }



	private boolean performClickInternal() {
        // Must notify autofill manager before performing the click actions to avoid     
        //scenarios where
        // the app has a click listener that changes the state of views the autofill 
        //service might
        // be interested on.
        notifyAutofillManagerOnClick();

        return performClick();
    }

  public boolean performClick() {
        // We still need to call this method to handle the cases where performClick() was 
        //called
        // externally, instead of through performClickInternal()
        notifyAutofillManagerOnClick();

        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);

        notifyEnterOrExitForAutoFillIfNeeded(true);

        return result;
    }

performClickInternal會執行 performClick()方法,

 performClick()會執行 li.mOnClickListener.onClick(this);  這個onclick就是我們定義的onclick事件

由於isFoucusableInTouchmode=true  所以導致focusTaken=true 

這樣performClickInternal()方法 就不會執行,

最終導致Button的onclick事件不執行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章