如何在android中单击时更改按钮颜色

vns*_*tty 1 android widget button

默认情况下,单击按钮时,橙色等颜色会短时间环绕按钮,表示单击了按钮.但是当按钮包含背景图像时,此功能无效.这也发生在列表视图中.为什么?有任何想法吗?TIA
我使用了setBackgroundColor(Color.BLUE); 但这里应用的颜色并没有消失......

Rav*_*yas 5

您需要使用选择器作为后台资源:

<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/button_focus" />
    <item android:drawable="@drawable/button_normal" />
</selector>
Run Code Online (Sandbox Code Playgroud)

此选择器使用3个单独的绘图来更改按钮的图像,您可以使用相同的图像进行按下和聚焦.

您需要将此XML放入drawables目录并将其用作按钮的背景.

有关其他解决方案,请参阅:标准Android按钮,颜色不同