繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM