2016-07-29 135 views
3

我有一個問題,直到還找不到解決方案!我用幾個鍵實現了一個自定義鍵盤。每個鍵都有一個背景圖像。我想改變按鍵本身的背景色顯示在下面的原裝鍵盤:Android自定義鍵盤更改背景顏色鍵按下

enter image description here

我不想有一個預覽,我想改變密鑰本身的背景顏色,當按鍵被按下時。下面是我的文件:

keyboard.xml

<?xml version="1.0" encoding="UTF-8"?> 
<android.inputmethodservice.KeyboardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/keyboard" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:keyPreviewOffset="10dp" 
android:keyPreviewLayout ="@layout/preview" 
android:keyTextColor="@color/colorAccent" 
android:keyBackground="@drawable/keybackground" 
android:background="#881f2023" 
/> 

keybackground.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:drawable="@drawable/cleanbuttonnormal" /> 
<item 
    android:state_pressed="true" 
    android:drawable="@drawable/cleandeactivate" /> 
<item 
    android:state_checkable="true" 
    android:drawable="@drawable/cleanbuttonnormal" /> 
<item 
    android:state_checkable="true" 
    android:state_pressed="true" 
    android:drawable="@drawable/cleandeactivate" /> 
<item 
    android:state_checkable="true" 
    android:state_checked="true" 
    android:drawable="@drawable/cleanbuttonnormal" /> 
<item 
    android:state_checkable="true" 
    android:state_checked="true" 
    android:state_pressed="true" 
    android:drawable="@drawable/keybackground" /> 
</selector> 

的背景顏色將變爲我的繪製,但是當我命中關鍵,它不會改變到按下狀態。背景保持不變。你可以幫我嗎?

在這裏我的自定義鍵盤佈局,啓用預覽顯示該按鈕被擊中。

enter image description here

與8的黑色按鈕應該得到黃色。預覽用於調試目的。

回答

1

問題出在您的州列表keybackground.xml。下面是從state list documentation摘錄:

在每個狀態變化,狀態列表遍歷從上到下 和匹配當前狀態下使用,在 選擇不是基於「最佳匹配的第一個項目, 「但只是符合國家最低標準的第一項 。

所以在你的情況下,每次都選擇​​3210。你應該改變項目的順序,以便最具體的項目先行。

+0

感謝亞歷山大的回覆!我理解這個問題,但我在哪裏改變這個?哪些文件首先被檢查?如何將我的特定background.xml文件設置爲第一個背景?我注意到我犯了一個錯字。我糾正了一次。我使用了cleanbuttonnormal的keybackground.xml。現在看起來像我的代碼。 – user3325230

+0

只需嘗試相反的順序! –

+0

謝謝!沒有意識到這個事實。你是對的,只是移動上面的檢查狀態,一切正常!謝謝! – user3325230

相關問題