我们正在实现自定义输入法服务,我们有兴趣接收光标更新事件。InputMethodSession 的 updateCursor() 文档说:“当目标输入字段的光标位置在其窗口内发生变化时调用此方法。通常不调用,但只有在输入方法请求时才会报告。 ”
输入法怎么能请求这个事件呢?
提前致谢,
安德烈
我们正在实现自定义输入法服务,我们有兴趣接收光标更新事件。InputMethodSession 的 updateCursor() 文档说:“当目标输入字段的光标位置在其窗口内发生变化时调用此方法。通常不调用,但只有在输入方法请求时才会报告。 ”
输入法怎么能请求这个事件呢?
提前致谢,
安德烈
看起来这是未实现的。在TextView
实现中,调用updateCursor
是有条件的InputMethodManager.isWatchingCursor
,它的定义如下:
/**
* Returns true if the current input method wants to watch the location
* of the input editor's cursor in its window.
*/
public boolean isWatchingCursor(View view) {
return false;
}
幸运的是,您可以使用 检测光标移动onUpdateSelection
,尽管它们将作为文本中的逻辑位置而不是 Rect 提供给您。
从 API 级别 21 (Lollipop) 开始,有一种方法可以做到:
覆盖onUpdateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo)
并打电话inputConnection.requestCursorUpdates(int cursorUpdateMode)
在你得到 inputConnection 之后。
onUpdateCursorAnchorInfo
每次光标位置改变时都会调用。