如何为Android自定义键盘中的每个键设置不同的键背景

How to set Different Key Background for every Key in Android Custom Keyboard

我正在使用自定义键盘,我需要在运行时或动态地为每个键设置不同的图像,但我不知道如何做到这一点。

我正在使用这个代码:

1
2
3
4
5
6
7
8
9
10
11
<com.keyboard.KeyBoard.LatinKeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.keyboard.KeyBoard"
android:id="@+id/keyboard"
android:keyBackground="@drawable/black_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:focusable="true"
android:background="@android:color/black"
android:focusableInTouchMode="true" />

现在我想在

中获取 android:keyBackground="@drawable/black_bg" 属性

1
2
3
4
@Override
public void onStartInputView(EditorInfo attribute, boolean restarting) {
    super.onStartInputView(attribute, restarting);  
}

因为我必须在此处动态更改键的图像背景,但我不知道如何做到这一点。


后来有一个重复的问题发布在
如何更改Android软键盘中任意键的键背景。它有一些很好的答案。