res\drawable\color_selector.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/red" android:state_pressed="true"></item>//按下
<item android:drawable="@drawable/green"></item>//松开
</selector>
res\values\color.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#f00</color>
<color name="green">#0f0</color>
<color name="blue">#00f</color>
<drawable name="red">#f00</drawable>
<drawable name="green">#00f</drawable>
</resources>
Button布局中引用方法android:background="@drawable/color_selector":
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/color_selector"
android:text="按下变颜色" />