[英]How to move the word cursor to a y position on the Android EditText text?
[英]How to get cursor position (x,y) in EditText android
如何在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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.