2011-09-27 92 views
2

我正在使用Eclipse來編寫android應用程序。我已經從Form Widgets選項卡添加了一些標準按鈕,併成功地讓他們打開顯示額外按鈕的新窗口。使按鈕看起來按下

我想按下按鈕,改變外觀並在選擇後繼續按下。

+0

你需要[切換按鈕(http://developer.android添加顏色的文件夾。 com/reference/android/widget/ToggleButton.html) –

回答

3

使用按鈕圖像這樣

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

創建XML文件中值

<?xml version="1.0" encoding="utf-8"?> 
<resources>  
    <color name="transparent">#00000000</color> 
</resources> 
1

可以使用選擇器

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

現在設置在XML按鈕的背景屬性此繪製,現在在編碼時按鈕被按下設置該標誌採取一個布爾標誌,並設置該按鈕的bacground(選擇圖像)並再次單擊重置標誌值並將imageBackground更改爲選擇器,那就是它!