这看起来很简单,但我无法禁用ImageButton。它会继续接收点击事件,并且它的外观不会像标准的Button那样改变。
SO上有一些similar questions,但它们对我没有帮助。
即使有这样一个非常简单的布局:
<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,那么它会像预期的那样工作。该按钮将变为禁用状态且无法单击。我不明白。有谁有主意吗?
发布于 2011-11-20 02:49:15
ImageButton具有不同的继承链,这意味着它不会扩展Button
ImageButton < ImageView < View
它会继续接收点击事件
下面是为View设置单击侦听器时会发生的情况
public void setOnClickListener(OnClickListener l) {
if (!isClickable()) {
setClickable(true);
}
mOnClickListener = l;
}因此,如果设置了侦听器,则android:clickable="false"将更改为android:clickable="true"。
及其外观不会像标准按钮那样改变
您应该为视图提供一个可绘制的状态列表,以便它可以基于android:enabled设置适当的图像。有这个吗?或者你的按钮只有一张图片?
编辑:您可以在StateListDrawable here上找到相关信息。android:state_enabled是您需要在列表中使用的内容,以便告诉操作系统在该状态下使用哪个映像。
EDIT2:由于您确实需要添加侦听器,因此可以在侦听器if (!isEnabled()) { return; } else { /* process the event */ }内部进行检查。
发布于 2013-01-03 04:46:59
下面是我用来禁用ImageButton并使其显示为灰色的代码:
/**
* Sets the specified image buttonto the given state, while modifying or
* "graying-out" the icon as well
*
* @param enabled The state of the menu item
* @param item The menu item to modify
* @param iconResId The icon ID
*/
public static void setImageButtonEnabled(Context ctxt, boolean enabled, ImageButton item,
int iconResId) {
item.setEnabled(enabled);
Drawable originalIcon = ctxt.getResources().getDrawable(iconResId);
Drawable icon = enabled ? originalIcon : convertDrawableToGrayScale(originalIcon);
item.setImageDrawable(icon);
}
/**
* Mutates and applies a filter that converts the given drawable to a Gray
* image. This method may be used to simulate the color of disable icons in
* Honeycomb's ActionBar.
*
* @return a mutated version of the given drawable with a color filter
* applied.
*/
public static Drawable convertDrawableToGrayScale(Drawable drawable) {
if (drawable == null) {
return null;
}
Drawable res = drawable.mutate();
res.setColorFilter(Color.GRAY, Mode.SRC_IN);
return res;
}只需调用setImageButtonEnabled()即可;唯一的缺点是您需要在此处包含图像的资源ID,因为不可能将转换后的图标还原为原始图标。
发布于 2012-10-01 19:55:47
如果要禁用图像按钮,请在单击事件时将属性"setEnabled“设置为false
例如:imgButton.setEnabled(false);
https://stackoverflow.com/questions/8196206
复制相似问题