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
中的州列表。以下是摘自 state list documentation:
During each state change, the state list is traversed top to bottom
and the first item that matches the current state is used—the
selection is not based on the "best match," but simply the first item
that meets the minimum criteria of the state.
所以在你的情况下每次都选择 <item android:drawable="@drawable/cleanbuttonnormal" />
。您应该更改项目的顺序,以便最具体的项目排在第一位。
我遇到了问题,至今找不到解决办法!我已经实现了一个带有多个键的自定义键盘。每个键都有一个背景图像。我想更改按下的键本身的背景颜色,如下面的原始键盘所示:
我不想预览,我想在按下键时更改键本身的背景颜色。这是我的文件:
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
中的州列表。以下是摘自 state list documentation:
During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.
所以在你的情况下每次都选择 <item android:drawable="@drawable/cleanbuttonnormal" />
。您应该更改项目的顺序,以便最具体的项目排在第一位。