如何获取光标下的行号
给定一个多行EditText,我如何知道光标所在位置的行号?
Given a multi-line EditText, how can I know the line number at cursor position?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
给定一个多行EditText,我如何知道光标所在位置的行号?
Given a multi-line EditText, how can I know the line number at cursor position?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
像这样的东西
textview.getPaint().measureText(textview.getText().toString()) / textView.getWidth();
这假设光标位于文本末尾,如果不是,则必须使用子字符串度量。
抱歉,我的 iPad 上的内容比较简短
Something like
textview.getPaint().measureText(textview.getText().toString()) / textView.getWidth();
This assumes the cursor is at the end of the text, if not you gotta use substring measure.
Sorry for brevity on my iPad
您的问题有一个解决方法。
这个问题可能会给您一些进一步的见解:如何获取ContentEditable区域中的行数和当前插入符行位置?
There exists a workaround for your problem.
This SO question may give you some further insights: How to get number of rows in ContentEditable area and current caret line position?
获取控件的文本并将其放入字符串中。循环字符串直到光标位置的索引并计算换行符的数量。
Take the text of the control and put it in a string. Loop over the string up to the index of the cursor position and count the number of newlines.