简体   繁体   English

Kotlin Custom Android Keyboard IME - InputMethodService 中的 onUpdateCursorAnchorInfo 方法不起作用

[英]Kotlin Custom Android Keyboard IME - method onUpdateCursorAnchorInfo from InputMethodService is not working

I'm developing a custom android keyboard in Kotlin and I have looked at this post for how to detect cursor movement/change when the user clicks on text editor.我正在 Kotlin 中开发一个自定义的 android 键盘,我查看了这篇文章,了解如何在用户单击文本编辑器时检测光标移动/更改。

Here is a snippet of my code of my custom keyboard class which extends InputMethodService.这是我的自定义键盘类的代码片段,它扩展了 InputMethodService。

class CustomKeyboard: InputMethodService() {
    override fun onCreateInputView(): View {
        // initiated keyboard
    }

    override fun onStartInputView(info: EditorInfo?, restarting: Boolean) {
        // call onUpdateCursorAnchorInfo() whenever cursor/anchor position is changed
        this.currentInputConnection.requestCursorUpdates(InputConnection.CURSOR_UPDATE_MONITOR)
        Log.i("--------------", "onStartInputView is called")
    
    }

    override fun onUpdateCursorAnchorInfo(cursorAnchorInfo: CursorAnchorInfo) {
        Log.i("--------------", "onUpdateCursorAnchorInfo is called")
    }
}

Then I ran this app on Android Studio emulator and tested on an EditText view in the following way:然后我在 Android Studio 模拟器上运行了这个应用程序,并通过以下方式在 EditText 视图上进行了测试:

  1. I click on the EditText view on my app我在我的应用程序上单击 EditText 视图
  2. I input many random characters in the EditText view我在 EditText 视图中输入了许多随机字符
  3. I click randomly on different characters in the EditText view我在 EditText 视图中随机单击不同的字符

The system log did print out "onStartInputView is called" when I first clicked on the EditText view.当我第一次单击 EditText 视图时,系统日志确实打印出“调用了 onStartInputView”。

But it did not print out "onUpdateCursorAnchorInfo is called" anywhere.但它没有在任何地方打印出“onUpdateCursorAnchorInfo is called”。

What I thought would happen was that "onUpdateCursorAnchorInfo is called" would have been printed multiple times as I clicked on different parts of the EditText view.我认为会发生的是,当我单击 EditText 视图的不同部分时,会多次打印“调用 onUpdateCursorAnchorInfo”。

However, when I changed to a different app and click on the text editor there, the Log did print out "onStartInputView is called" and "onUpdateCursorAnchorInfo is called" once.但是,当我更改为另一个应用程序并单击那里的文本编辑器时,日志确实打印了一次“onStartInputView is called”和“onUpdateCursorAnchorInfo is called”。

But when I repeat step 1~3 on that app it did not work either但是当我在该应用程序上重复步骤 1~3 时,它也不起作用

I have no idea why this behaviour occurs.我不知道为什么会发生这种行为。

Any help would be appreciated.任何帮助,将不胜感激。

Okay never mind somehow it works now.好吧,没关系,它现在可以工作了。 It must have been a bug or something.它一定是一个错误或什么的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM