使用XML實現按鈕改變焦點設定背景圖,獲得焦點時,獲得焦點並按下,失去焦點時,預設時...
建立res/drawable/button.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 獲得焦點時 --><itemandroid:state_focused="true"android:state_pressed="false"android:drawable="@drawable/focusimage"/><!-- 獲得焦點並按下 --><itemandroid:state_focused="true"android:state_pressed="true"android:drawable="@drawable/clickimage"/><!-- 失去焦點時 --><itemandroid:state_focused="false"android:state_pressed="true"android:drawable="@drawable/clickimage"/><!-- 預設時 --><item android:drawable="@drawable/normal"/></selector>
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_marginTop="20dp" android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="不同狀態的Button" android:background="@drawable/button"/></LinearLayout>