如何在EditText android中获取光标位置(x,y)

hie*_*ubk 12 android

如何EditText在Android中获取光标位置x,y ?(其中x是行#,y是列#)

Ran*_*ku' 36

int pos = editText.getSelectionStart();
Layout layout = editText.getLayout();
int line = layout.getLineForOffset(pos);
int baseline = layout.getLineBaseline(line);
int ascent = layout.getLineAscent(line);
float x = layout.getPrimaryHorizontal(pos);
float y = baseline + ascent;
Run Code Online (Sandbox Code Playgroud)

然后你得到光标像素的x,y位置.