我试图使用ImageView作为Button,我希望能够改变每当按钮的按下的图像。我有一个OnClickListener集,但我有什么做一下当用户的手指不再下降,在按钮上?如何恢复到原来的形象?

有帮助吗?

解决方案

用户这个代替:

View.OnTouchListener
abstract boolean onTouch(View v, MotionEvent event)

事件参数会让你知道,如果它的ACTION_DOWNACTION_UP

其他提示

这样做的正确方法是扩展Button类,或者如果你只想要更改按钮图像,你可以通过XML设置的样式。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_default_normal_disable" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/btn_default_pressed" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_default_selected" />
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_focused="true"
        android:drawable="@drawable/btn_default_normal_disable_focused" />
    <item
         android:drawable="@drawable/btn_default_normal_disable" />
</selector>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top