禁用图像按钮
这看起来很简单,但我无法禁用 ImageButton
。它继续接收点击事件,并且其外观不会像标准按钮那样改变。
有一些类似的问题,但它们对我没有帮助。
即使有一个非常简单的布局,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/btn_call"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:clickable="false"
android:enabled="false"
android:src="@android:drawable/sym_action_call" />
</LinearLayout>
该按钮仍然处于启用状态,我可以单击它。
奇怪的是,如果我将 ImageButton
更改为简单的 Button
,那么它就会按预期工作。该按钮将被禁用并且无法单击。我不明白。有人有想法吗?
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 would.
There are some similar questions on SO, but they don't help me.
Even with a very simple layout like this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/btn_call"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:clickable="false"
android:enabled="false"
android:src="@android:drawable/sym_action_call" />
</LinearLayout>
The button is still enabled and I can click it.
What's strange is that if I change the ImageButton
to a simple Button
, then it works as expected. The button becomes disabled and unclickable. I don't understand. Does anyone have an idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
以下是我用来禁用
ImageButton
并使其显示为灰色的代码:只需调用
setImageButtonEnabled()
;唯一的缺点是您需要在此处提供图像的资源 ID,因为无法将转换后的图标恢复为原始图标。Here's the code I use to disable an
ImageButton
and make it look grayed out:Simply call
setImageButtonEnabled()
; the only downside is you need the image's resource ID in here because it's not possible to revert a transformed icon back into the original.ImageButton
具有不同的继承链,这意味着它不扩展Button
:ImageButton
ImageButton
ImageButton
ImageView
<查看
以下是为
View
设置点击侦听器时发生的情况:因此,如果您设置侦听器,
android:clickable="false"
会更改为android:clickable="true"
。您应该向视图提供一个可绘制状态列表,以便它可以根据
android:enabled
设置适当的图像。你有这个吗?或者您的按钮只有唯一的图像?编辑:您可以在此处找到有关 StateListDrawable 的信息。
android:state_enabled
是您需要在列表中使用的内容,以便告诉操作系统该状态使用什么图像。EDIT2:由于您确实需要添加一个侦听器,因此您可以在侦听器内部进行检查
if (!isEnabled()) { return; } else { /* 处理事件 */ }
.ImageButton
has different inheritance chain meaning it does not extendButton
:ImageButton
<ImageView
<View
Here is what happens when you set a click listener for the
View
:So if you set a listener the
android:clickable="false"
changes toandroid:clickable="true"
.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 */ }
.如果要禁用图像按钮,请在单击事件时将属性“setEnabled”设置为 false
例如:
imgButton.setEnabled(false);
if you want to disable an image button,on click event, set the the property "setEnabled" to false
Ex:
imgButton.setEnabled(false);
确保您的视图层次结构中没有具有相同 id 的视图,并且您没有向该视图添加任何点击侦听器。
Make sure there is no view with same id in your view hierarchy and you do not add any click listener to that view.
利用Oleg Vaskevich的答案。可以为 Kotlin 做出答案。
为 ImageButton 创建一个扩展函数,这样:
你就可以减少对提供上下文
Taking advantage of the Oleg Vaskevich's answer. Can be made an answer for Kotlin.
Make a Extension Function for ImageButton, this way:
And you get a little less reliant on providing Context
我设法构建了一个受 Oleg Vaskevich 的 答案 启发的解决方案,但不需要将可绘制资源 ID 传递给 setEnabled()。
这是实用程序模块内部的 Kotlin 代码:
它可以像这样使用:
I managed to build a solution inspired by Oleg Vaskevich's answer, but without the need to pass drawable resource ID to setEnabled().
Here is Kotlin code, inside of utility module:
It can be used like this:
正如其他答案所说,您不能像禁用
Button
那样在布局 XML 中禁用ImageButton
,但您可以在运行时以相同的方式禁用这两者:在 Java 中
:这两种情况下按钮都被禁用——没有点击事件到达它的
onClickListener
。您还可以更改禁用的
ImageButton
的图标颜色,就像更改禁用的Button
上的文本颜色一样,假设该图标是可着色的。在布局 XML 中:
现在,
Button
或ImageButton
上的setEnable(boolean)
会根据中的状态更改文本或图标颜色>button_color_selector.xml
As other answers have said, you cannot disable an
ImageButton
in the layout XML as you can aButton
, but you can disable both the same way at runtime:In Java:
In both cases the button is disabled -- no click events get to its
onClickListener
.You can also change the icon color of the disabled
ImageButton
the same way you change the text color on a disabledButton
, assuming the icon is tintable.In the layout XML:
Now
setEnable(boolean)
on theButton
orImageButton
changes the text or icon color according to the states in yourbutton_color_selector.xml
为了改进 Ivan 2018 年的答案:这是一个更简单的方法(Kotlin):
To improve on Ivan's 2018 answer: This is a much simpler method (Kotlin):