如何在Java中更改自定义Android键盘的键标签

时间:2019-03-29 12:13:39

标签: java android-softkeyboard custom-keyboard android-input-method

我想在运行时通过Java更改自定义键盘上的键标签。我在任何地方都找不到任何解决方案。

java文件 ...

公共类MyKeyboardService扩展了InputMethodService实现的KeyboardView.OnKeyboardActionListener {

public final static int keyToChange = 257;


KeyboardView kv;
Intent intent;

void updateInputView() {
    if (kv == null)
        return;
    Keyboard currentKeyboard = kv.getKeyboard();
    List<Keyboard.Key> keys = currentKeyboard.getKeys();
    keys.get(keyToChange).label = "Change Label";
    kv.invalidateKey(keyToChange);
}

@Override
public View onCreateInputView() {

    checkIfFirstRun();

    KeyboardView kv = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null);
    Keyboard keyboard = new Keyboard(this, R.xml.layout_keys);

    kv.setKeyboard(keyboard);
    kv.setPreviewEnabled(false);
    kv.setOnKeyboardActionListener(this);
    updateInputView();


    return kv;

...

xml文件

    <Key
        android:codes="257"
        android:keyWidth="60%p"/>

现在,我仍然在钥匙上得到空白标签。 我希望它显示“更改标签”

1 个答案:

答案 0 :(得分:0)

List <>。get()返回具有特定索引的键,而不是具有特定代码的键。例如,List <>。get(0)返回第一个键。