Android 禁用图像按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3118108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
disable an ImageButton?
提问by Pedro
I wanted to leave an ImageButton
is disabled(not clickable) but have used android: enabled = "false"
and does't work.
我想离开一个ImageButton
被禁用(不可点击)但已经使用android: enabled = "false"
并且不起作用。
Does anyone know how to disablean ImageButton
?
没有人知道如何禁用的ImageButton
?
回答by GKeps
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.
如果您想将按钮显示为禁用(如果您在 xml drawable 文件中设置了该按钮),同时执行setClickable(false)
AND 即可setEnabled(false)
。
回答by Cristian
You can use the android:clickable
attribute on the XML, or the setClickable(boolean)
method from your code.
您可以使用android:clickable
XML 上的属性或setClickable(boolean)
代码中的方法。
回答by user2924714
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.
为 设置点击侦听器时ImageButton
,在底层android 会将属性 clickable 重置为true
。这就是为什么android:clickable="false"
在 xml 中设置没有帮助的原因。
In addition, setting the attribute android:enabled="false"
in the xml didn't work for me as well.
此外,android:enabled="false"
在 xml 中设置属性对我也不起作用。
What did work is only setting via the code:
什么工作只是通过代码设置:
ImageButton mBtnDelayCall = (ImageButton)v.findViewById(R.id.btnCallDelay);
mBtnDelayCall.setEnabled(false);
回答by Javatar
If you want to disableand "grey out"the image, I use the following (Kotlin):
如果你想禁用和“灰色”图像,我使用以下(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:
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"/>
Notethat the your background color will define the color of the disabled state. Depends on your desired result.
请注意,您的背景颜色将定义禁用状态的颜色。取决于你想要的结果。
回答by Yuri Misyac
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"
.
ImageButton
likeImageView
没有android:enabled="false"
属性,因为它是 的属性TextView
。如果您想enable = false
在 XML 中进行制作,ImageButton
则必须添加android:focusable="false"
和android:clickable="false"
。