簡體   English   中英

Android自定義鍵盤更改按下背景顏色鍵

[英]Android Custom Keyboard Change Background color key pressed

我有一個問題,直到找不到解決方案! 我已經實現了一個帶有幾個鍵的自定義鍵盤。 每個鍵都有一個背景圖像。 我想改變按鍵本身的背景顏色,如下面的原始鍵盤所示:

在此輸入圖像描述

我不想進行預覽,我想在按下按鍵時更改按鍵本身的背景顏色。 這是我的文件:

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>

背景顏色將更改為我的drawable,但是當我按下該鍵時,它將不會更改為按下狀態。 背景保持不變。 請你幫助我好嗎?

在這里我的自定義鍵盤布局,啟用預覽以顯示按鈕被擊中。

在此輸入圖像描述

帶有8的黑色按鈕應該變黃。 預覽用於調試目的。

問題在於keybackground.xml的狀態列表。 以下是州列表文檔的摘錄:

在每次狀態更改期間,狀態列表從上到下遍歷,並且使用與當前狀態匹配的第一個項目 - 選擇不基於“最佳匹配”,而只是滿足州的最低標准的第一個項目。

所以在你的情況下,每次都會選擇<item android:drawable="@drawable/cleanbuttonnormal" /> 您應該更改項目的順序,以便首先使用最具體的項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM