禁用的ImageButton [英] Disable an ImageButton

查看:103
本文介绍了禁用的ImageButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来很容易,但我不能禁用的ImageButton 。它继续接受点击事件,其外观不会改变像一个标准的按钮会。

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.

有一些类似的问题对SO,但他们不帮我。

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.

有什么奇怪的是,如果我改变了的ImageButton 来一个简单的按钮,然后按预期工作。该按钮就退出,无法点击。我听不懂。有没有人有一个想法?

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?

推荐答案

的ImageButton 有不同的继承链这意味着它不会延长按钮

ImageButton has different inheritance chain meaning it does not extend Button:

的ImageButton &LT; 的ImageView &LT; 查看

ImageButton < ImageView < View

继续接收单击事件

下面是当你设定一个点击监听器会发生什么查看

Here is what happens when you set a click listener for the View:

public void setOnClickListener(OnClickListener l) {
    if (!isClickable()) {
        setClickable(true);
    }
    mOnClickListener = l;
}

因此​​,如果您设置一个侦听器的机器人:可点击=假修改机器人:可点击=真正的

和它的外观没有改变这样一个标准的按钮会

and its appearance don't change like a standard Button would

您应该提供一个可绘制状态列表视图,以便它可以设置基于安卓合适的图像:启用。你有这个吗?或者你拥有的唯一形象,为您的按钮?

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?

编辑:您可以找到StateListDrawable <一个信息href="http://developer.android.com/intl/zh-CN/guide/topics/resources/drawable-resource.html#StateList">here. 安卓state_enabled 是你需要在列表中使用,以告诉操作系统使用该状态什么样的形象

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:(!IsEnabled的()),因为你真的需要添加一个监听器,你可以做一个检查监听器内如果{返回; }其他{/ *处理该事件* /}

Since you really need to add a listener you can make a check inside of the listener if (!isEnabled()) { return; } else { /* process the event */ }.

这篇关于禁用的ImageButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆