如何在Android的EditText
中获取光标位置x、y坐标(其中x为行号,y为列号)?
如何在Android的EditText
中获取光标位置x、y坐标(其中x为行号,y为列号)?
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;
你可以在那里获取光标的x和y像素位置。
// Get the current selection's line number
int line = edittext.getLayout().getLineForOffset(edittext.getSelectionStart());
// Find the index for the start of the selected line. Subtract that from the current selection's index
int column = edittext.getSelectionStart() - edittext.getLayout().getLineStart(line);