2014-10-16 327 views
1

我想要一個具有背景圖片的按鈕,當我點擊該按鈕時,按鈕的背景或顏色發生變化(並且我們轉到其他活動)。 告訴我怎麼做到這一點。點擊時更改按鈕背景

回答

0

創建xml文件並將其保存在你的項目中drawable一個文件夾,讓bg.xml,設置這個作爲你Button

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
     <item android:drawable="@drawable/button_pressed" android:state_pressed="true" /> 
     <item android:drawable="@drawable/button_notpressed" /> 
</selector> 

其中button_pressedbutton_notpressed背景都是你想要的圖片上顯示的點擊轉換和button

在你活動的xml文件普通視圖,設置button作爲bg.xml的背景

<Button 
     android:id="@+id/btn" 
     android:layout_width="150dp" 
     android:layout_height="60dp" 
     android:textSize="20dp" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="450dp" 
     android:background="@drawable/bg" <--! set it like this--> 
     android:text="Submit" 
     android:textColor="#FFFFFF" /> 
+0

感謝ü非常 – Paul 2014-10-16 13:21:53

+0

你能解釋一下我的選擇(是什麼,能這樣做) – Paul 2014-10-16 13:25:37

+0

[請參見本(http://developer.android.com/reference/java/nio/channels/Selector.html ) – nobalG 2014-10-16 13:27:49