如何在Android的EditText
中获取光标位置x,y? (其中x是行#,y是列#)
答案 0 :(得分:35)
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位置。
答案 1 :(得分:1)
您可以像这样找到当前选择的行号和列:
// 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);