2012-06-22 56 views
0

我的佈局中有4個按鈕 - 它們是4種可能的視圖內容之間的切換器。任何時候按下其中一個按鈕,我需要突出顯示它(只需加載其他背景,其他圖像並更改文本的顏色)。麻煩的是,我需要在觸摸按鈕(不是點擊後)和按下其他按鈕之前完成此操作。對onClick()方法的簡單使用不會很好,因爲我必須檢查其他按鈕是否已經高亮顯示,並且只有在點擊後纔會進行更改。任何好的解決方案?Android:突出顯示按鈕,直到按下其他按鈕

+0

對此有什麼好運? – Barak

+0

不幸的是,這是行不通的。釋放按鈕後,按鈕會改變顏色。我也嘗試了android:state_pressed和android:state_focused的不同組合 - 沒有幫助。 –

+0

UPD:解決方案是設置button.setFocusableInTouchMode(true); –

回答

1

onFocusChange聽衆怎麼樣?

button.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      // set the background, image and color for focused 
     } else { 
      // set the background, image and color back to normnal 
     } 
    } 
}); 

爲每個按鈕設置一個按鈕,你應該全部設置。