Disable an ImageButton

后端 未结 7 1801
小蘑菇
小蘑菇 2020-12-14 01:51

This looks easy, but I\'m not able to disable an ImageButton. It continues to receive click events, and its appearance don\'t change like a standard Button woul

7条回答
  •  温柔的废话
    2020-12-14 01:59

    ImageButton has different inheritance chain meaning it does not extend Button:

    ImageButton < ImageView < View

    It continues to receive click events

    Here is what happens when you set a click listener for the View:

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

    So if you set a listener the android:clickable="false" changes to android:clickable="true".

    and its appearance don't change like a standard Button would

    You should supply a drawable state list to the view so it could set an appropriate image based on android:enabled. Do you have this? Or you have the only image for your button?

    EDIT: You can find info on StateListDrawable here. android:state_enabled is what you need to use in the list in order to tell the OS what image to use for that state.

    EDIT2: Since you really need to add a listener you can make a check inside of the listener if (!isEnabled()) { return; } else { /* process the event */ }.

提交回复
热议问题