从源码的角度分析Android中setClickable()和setEnable()的区别

做Android开发的朋友,无论是在Java代码中还是在XML文件中,对控件的clickable和enable都很熟悉,那么这两个属性对控件到底有什么影响,今天我们从源码的来解答这个问题

首先,拿最常见的Button举例子,因为Button是最终继承于View,当我们点击按钮时,给按钮增加监听

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG","onClick execute");
            }
        });

首先会调用dispatchTouchEvent()方法,进入View查看关键源码如下:

        if (onFilterTouchEventForSecurity(event)) {
            //noinspection SimplifiableIfStatement
            ListenerInfo li = mListenerInfo;
            if (li != null && li.mOnTouchListener != null
                    && (mViewFlags & ENABLED_MASK) == ENABLED
                    && li.mOnTouchListener.onTouch(this, event)) {
                result = true;
            }

            if (!result && onTouchEvent(event)) {
                result = true;
            }
        }

1、当设置setEnable(false)时,我们进入onTouchEvent后,看下面的代码:

if ((viewFlags & ENABLED_MASK) == DISABLED) {
            if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
                setPressed(false);
            }
            // A disabled view that is clickable still consumes the touch
            // events, it just doesn't respond to them.
            return (((viewFlags & CLICKABLE) == CLICKABLE
                    || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
                    || (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE);
        }

会调用setPressed(false);这是按钮的状态将会变成不可点击状态,所以这时我们点击也不会有任何反应
接着,但还是消费了这次事件,也就是上面代码注释的内容

2、当我们不对setEnable做处理,调用setClickable(false)后,步骤还是如上,只不过这次代码会接着进入点击状态判断的内容

if (((viewFlags & CLICKABLE) == CLICKABLE ||
                (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||
                (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {
            switch (action) {
                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 && !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)) {
                                    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();
                    }
                    mIgnoreNextUpEvent = false;
                    break;

这是MotionEvent.ACTION_UP的内容,注意方法的判断条件,当我们设置setClickable(false)后,CLICKABLE=false,那么这个循环体的内容就不会执行,而我们点击按钮时,日志也不会打印,那么我们可以猜想,onClick就是在循环体内执行的,我们在源码中并没有找到onClick,但是找到了 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;
    }

在这里,我们终于找到了onClick,这里mOnClickListener就是我们在调用 mButton.setOnClickListener()时进行的初始化,这就证明了我们的猜想,好,你试了如下代码

public class ViewClickDemo extends AppCompatActivity {

    private Button mButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.click_view);
        mButton = (Button) findViewById(R.id.click_view);
        mButton.setClickable(false);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG","onClick execute");
            }
        });

    }

}

之后,你会骂人的,为什么还打印日志,我不是已经设置为false了吗,但是当你把 mButton.setClickable(false);放在mButton.setOnClickListener()之后,你会发现日志就不打印了,这是为什么,我们还是看setOnClickListener()的源码:

    public void setOnClickListener(@Nullable OnClickListener l) {
        if (!isClickable()) {
            setClickable(true);
        }
        getListenerInfo().mOnClickListener = l;
    }

看到这里我相信你已经明白了,当我们在之前调用setClickable()把clickable设置成false后,在进入setOnClickListener会对clickable状态进行判断,如果为false,会再次把clickable设置为true;所以要设置起作用,需要把setClickable()放在setOnClickListener之后。

到这儿就分析为了,为了给不懂得小伙伴参考一下,如果哪儿有不对的地方,也请小伙伴赐教,( ^_^ )/~~拜拜

你可能感兴趣的:(Android)