gpt4 book ai didi

Android 自定义键盘更改背景颜色键按下

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:42 25 4
gpt4 key购买 nike

我遇到了一个问题,至今找不到解决方案!我已经实现了一个带有多个键的自定义键盘。每个键都有一个背景图像。我想更改按下的键本身的背景颜色,如下面的原始键盘所示:

enter image description here

我不想预览,我想在按下键时更改键本身的背景颜色。这是我的文件:

键盘.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 的黑色按钮应该变成黄色。预览用于调试目的。

最佳答案

问题出在您在 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" />每次都被选中。您应该更改项目的顺序,以便最具体的项目排在最前面。

关于Android 自定义键盘更改背景颜色键按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38648852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com