Android图片按钮在点击时不会闪烁

时间:2011-04-28 09:47:56

标签: android

我实现了一个ImageButton。一切顺利,除非我按下它,它不会“闪烁”,然后继续(到另一个活动)。 Android是否具有ImageButton的内在“闪存”,或者我必须在onClickEvent中明确地编写/动画?或使用选择器?

提前感谢您的帮助。

5 个答案:

答案 0 :(得分:2)

如果你让你的ImageButton保持其背景并且不将其设置为null,它将像普通按钮一样闪烁,并且在点击时会闪烁,就像其他按钮一样。隐藏背景的方式仍在那里:

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="1dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:paddingTop="1dp"
    android:src="@drawable/squareicon" />

填充不会让背景可见,并使按钮像其他按钮一样。

答案 1 :(得分:1)

如果您有正常按钮的图像和按下状态的一个图像,则应使用选择器。我认为这是最简单的方法。

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

答案 2 :(得分:0)

答案 3 :(得分:0)

结束以编程方式执行它,因为我在使用“选择器”时遇到问题,Eclipse抱怨“无法解析文件”。

public void flashBtn (final ImageButton myBtnToFlash){
    myBtnToFlash.setBackgroundResource(R.drawable.glossy_button_green_rectangle);
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
         public void run() { 
             myBtnToFlash.setBackgroundResource(0);
         } 
    }, 50);

}

答案 4 :(得分:0)

对于ImageButtons,您不应该设置它的背景。将图像设置为“src”,然后ImageButton仍将具有默认的闪烁属性。

在这里,我想,你正在为你的ImageButton设置背景,我认为这不是必需的。

相关问题