禁用ImageButton?

问题描述 投票:28回答:5

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

有谁知道如何禁用ImageButton

android imagebutton
5个回答
25
投票

如果你想将按钮显示为禁用(如果你在xml可绘制文件中设置了那个),那么setClickable(false)setEnabled(false)都会做到这一点。


14
投票

您可以在XML上使用android:clickable属性,或在代码中使用setClickable(boolean)方法。


3
投票

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

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

什么工作只通过代码设置:

ImageButton mBtnDelayCall = (ImageButton)v.findViewById(R.id.btnCallDelay);
mBtnDelayCall.setEnabled(false);

1
投票

ImageButton这样的ImageView没有android:enabled="false"属性,因为它是TextView的属性。如果你想用enable = false的XML格式化ImageButton,你必须添加android:focusable="false"android:clickable="false"


1
投票

如果你想禁用并“灰显”图像,我使用以下(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"/>

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

© www.soinside.com 2019 - 2024. All rights reserved.