How to change key label of the custom android keyboard in java

问题描述 投票:0回答:1

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

java文件


public class MyKeyboardService extends InputMethodService implements 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"/>

现在,我的钥匙上仍然有空白标签。 我想让它显示“更改标签”

java android-softkeyboard custom-keyboard android-input-method
1个回答
-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.