disable an ImageButton?

前端 未结 5 738
旧时难觅i
旧时难觅i 2020-12-10 10:29

I wanted to leave an ImageButton is disabled (not clickable) but have used android: enabled = \"false\" and does\'t work.

相关标签:
5条回答
  • 2020-12-10 10:49

    If you want to disable and "grey out" the image, I use the following (Kotlin):

    Disable:

    chevron_left.imageAlpha = 75 // 0 being transparent and 255 being opaque
    chevron_left.isEnabled = false
    

    Enable:

    chevron_left.imageAlpha = 255
    chevron_left.isEnabled = true
    

    XML:

    <ImageButton
                android:id="@+id/chevron_left"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginBottom="4dp"
                android:layout_marginStart="4dp"
                android:background="?android:attr/selectableItemBackgroundBorderless"
                android:src="@drawable/chevron_left"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"/>
    

    Note that the your background color will define the color of the disabled state. Depends on your desired result.

    0 讨论(0)
  • 2020-12-10 10:51

    ImageButton like ImageView does not have android:enabled="false" attribute, because it is attribute of TextView. If you want make enable = false in XML for ImageButton you have to add android:focusable="false" and android:clickable="false".

    0 讨论(0)
  • 2020-12-10 11:02

    When setting a clicklistener for the ImageButton, under the hood android resets the attribute clickable to true. That's why setting android:clickable="false" in xml is not helpful.

    In addition, setting the attribute android:enabled="false" in the xml didn't work for me as well.

    What did work is only setting via the code:

    ImageButton mBtnDelayCall = (ImageButton)v.findViewById(R.id.btnCallDelay);
    mBtnDelayCall.setEnabled(false);
    
    0 讨论(0)
  • 2020-12-10 11:10

    You can use the android:clickable attribute on the XML, or the setClickable(boolean) method from your code.

    0 讨论(0)
  • 2020-12-10 11:13

    If you want to show the button as disabled (if you have that set up in an xml drawable file) doing both setClickable(false) AND setEnabled(false) will do the trick.

    0 讨论(0)
提交回复
热议问题