自定义 KeyboardView 上的 keyOutputText 属性不输出任何文本

keyOutputText attribute on a custom KeyboardView doesn't output any text

我正在尝试制作自定义键盘,但它没有输出我在 Key 节点上的 keyOutputText 中设置的文本值。

这里是activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

键盘布局如下:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="0px"
    android:keyHeight="60dp"
    android:keyWidth="15%p"
    android:verticalGap="0px">

    <Row>
        <Key
            android:keyLabel="test"
            android:keyOutputText="test" />
    </Row>
</Keyboard>

这是 Java class:

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.view.View;
import android.view.inputmethod.InputConnection;

public class TestKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

    private KeyboardView kv;
    private Keyboard keyboard;

    @Override
    public View onCreateInputView() {
        kv = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.keyboard);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    @Override
    public void onPress(int primaryCode) {
    }

    @Override
    public void onRelease(int primaryCode) {
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
    }

    @Override
    public void onText(CharSequence text) {

    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }
}

我还没有真正找到任何关于这方面的好教程,所以我不确定我是否没有实现任何我应该实现的代码?如有必要,我将提供更多代码和资源。一切正常,应用程序构建并启动,我可以将我当前的键盘设置为这个键盘,但是当我实际按下该键时没有任何反应。它的响应就像我按下它一样,但没有输出任何文本。

当你按下一个键时,onKey(int primaryCode, int[] keyCodes)方法会被调用,你的方法体是空的,所以什么也不会发生!

primaryCode参数实际上是你在键盘xml文件中定义的按下Keyandroid:codes属性,而不是android:keyOutputText!使用正确的 ASCII 代码填充android:codes 属性。

为了提交文本,您可以使用像这样的简单代码块:

@Override
public void onKey(int primaryCode, int[] keyCodes) {
  InputConnection ic = getCurrentInputConnection();
  ic.commitText(String.valueOf(code),1);
}

IME 是一个相当广泛的话题,您可以在 Internet 上找到很多关于它的信息,请查看此 tutorials,它对我帮助很大。

顺便你可以找到一些源代码here

祝你好运!

原来我只需要实现 onText 方法。

@Override
public void onText(CharSequence text) {
    getCurrentInputConnection().commitText(text, 1);
}