android - 如何获取(或制作)对 Android EditText 的 InputConnection 的引用

标签 android android-edittext keyboard

我正在制作一个我想包含在应用程序中的自定义键盘。我已经知道how to make a system keyboard .我不想这样做,因为它需要用户安装。

每当用户按下键盘上的一个键时,它应该将键文本发送到当前具有焦点的任何 EditText(如果有)。

documentation states

An editor needs to interact with the IME, receiving commands through this InputConnection interface, and sending commands through InputMethodManager.

如下图所示(其中 ViewEditText)。

enter image description here

这听起来像是我应该使用输入连接来与 EditText 通信。所以我的问题是,我的自定义键盘 View 如何获得对当前聚焦的 EditText 的输入连接的引用。或者它如何启动该连接?

相关

最佳答案

正如@pskink 在评论中提到的,您可以使用

InputConnection ic = editText.onCreateInputConnection(new EditorInfo());

获取对 EditText 的输入连接的引用。

EditText 通过添加监听器获得焦点时,它可以传递给自定义键盘。

// get the input connection from the currently focused edit text
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            InputConnection ic = editText.onCreateInputConnection(new EditorInfo());
            keyboard.setInputConnection(ic); // custom keyboard method
        }
    }
});

关于android - 如何获取(或制作)对 Android EditText 的 InputConnection 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44945918/

相关文章:

android - 在 Kotlin 中循环字符串并用新行替换句号

android - 有没有办法跟踪这个异常 : focus search returned a view that wasn't able to take focus

macos - 如何在 osx 上的/dev 中找到键盘

android - Android中的插页式广告-Gradle配置

android - 错误 : undefined reference to 'av_free_packet(AVPacket*)' when use NDK to compile ffmpeg

android - 获取不同 fragment 的 EditText 值

android - Android 中 RelativeLayout 问题中的 EditText

c++ - SDL_Event->key.keysym.sym 符号键(冒号、双引号、问号……)

Swift 让键盘适应设备屏幕

android - 如何将 PdfDocument 对象保存到 Android 中的文件中?