我该如何获取EditText中光标的坐标?这里不是要获取光标位置,而是要获取光标在EditText中的坐标。
在我的情况下,当我通过KeyEvent向EditText发送数字时,光标位置(getSelectionStart)会改变,但其位置始终在EditText的右侧。
我想知道光标的坐标(EditText的右侧)。
我该如何获取EditText中光标的坐标?这里不是要获取光标位置,而是要获取光标在EditText中的坐标。
在我的情况下,当我通过KeyEvent向EditText发送数字时,光标位置(getSelectionStart)会改变,但其位置始终在EditText的右侧。
我想知道光标的坐标(EditText的右侧)。
回答有点晚了 :), 但是从API level 21 (Lollipop)开始,可以这样做:
在你的InputMethodService中覆盖onUpdateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo)
并在获取到inputConnection之后使用CURSOR_UPDATE_MONITOR标志调用inputConnection.requestCursorUpdates(int cursorUpdateMode)
每次光标位置改变时,onUpdateCursorAnchorInfo
将被调用。您可以通过cursorAnchorInfo.getInsertionMarkerHorizontal()
(x)和cursorAnchorInfo.getInsertionMarkerTop()
(y)访问光标的右上角坐标。