33

我想留下一个ImageButton禁用(不可点击),但已经使用android: enabled = "false"并且不起作用。

有谁知道如何禁用一个ImageButton

4

5 回答 5

27

如果您想将按钮显示为禁用(如果您在 xml 可绘制文件中设置了该按钮),则同时执行setClickable(false)AND即可setEnabled(false)

于 2011-07-19T16:19:45.993 回答
16

您可以使用android:clickableXML 上的属性或setClickable(boolean)代码中的方法。

于 2010-06-25T12:54:28.650 回答
8

为 设置 clicklistener 时ImageButton,在后台 android 会将 clickable 属性重置为true. 这就是为什么android:clickable="false"在 xml 中设置没有帮助。

此外,android:enabled="false"在 xml 中设置属性对我也不起作用。

起作用的只是通过代码设置:

ImageButton mBtnDelayCall = (ImageButton)v.findViewById(R.id.btnCallDelay);
mBtnDelayCall.setEnabled(false);
于 2015-11-30T09:26:41.667 回答
8

如果您想禁用图像并将其“变灰”,我使用以下(Kotlin):

禁用:

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

使能够:

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"/>

请注意,您的背景颜色将定义禁用状态的颜色。取决于你想要的结果。

于 2018-10-08T15:28:43.397 回答
0

ImageButtonlikeImageView没有android:enabled="false"属性,因为它是 的属性TextView。如果你想enable = false在 XML 中生成,ImageButton你必须添加android:focusable="false"android:clickable="false".

于 2018-04-04T07:06:19.187 回答