Android EditText的光标坐标(绝对位置) [英] Android EditText's cursor coordinates (absolute position)

查看:1312
本文介绍了Android EditText的光标坐标(绝对位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取EditText中光标的坐标?我不是要在这里获取光标位置,而是要获取光标的EditText坐标.

How can I get the coordinates of the cursor in the EditText ? I am not trying to get the cursor position here but the EditText coordinates of the cursor.

在我的情况下,当我通过KeyEvent将数字发送到EditText时,光标位置(getSelectionStart)发生了变化,但其位置始终在EditText的右侧.

In my case, when I send a number via KeyEvent to the EditText, the cursor position (getSelectionStart) is changed, but its location is always at the right of the EditText.

我想知道光标的坐标(EditText的右边).

I want to know the coordinates of the cursor (the right of the EditText).

推荐答案

答案有点晚:),但是从API级别21(棒棒糖)开始,有一种方法可以做到:

It's a little bit late answer :), but from API level 21 (Lollipop) there is a way to do it:

  • 覆盖 在您的InputMethodService

  • Override onUpdateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo) in your InputMethodService

并调用 inputConnection.requestCursorUpdates(int cursorUpdateMode) 在获得inputConnection后带有CURSOR_UPDATE_MONITOR标志.

and call inputConnection.requestCursorUpdates(int cursorUpdateMode) with CURSOR_UPDATE_MONITOR flag after you got the inputConnection.

每次光标位置更改时,都会调用onUpdateCursorAnchorInfo.您可以通过cursorAnchorInfo.getInsertionMarkerHorizontal()(x)和cursorAnchorInfo.getInsertionMarkerTop()(y)访问光标的右上角坐标.

The onUpdateCursorAnchorInfo will be called every time the cursor's position has changed. You can access the cursor's top right coordinates by cursorAnchorInfo.getInsertionMarkerHorizontal() (x) and cursorAnchorInfo.getInsertionMarkerTop() (y).

这篇关于Android EditText的光标坐标(绝对位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆