如何获取光标下的行号

发布于 2024-09-29 13:38:11 字数 37 浏览 0 评论 0原文

给定一个多行EditText,我如何知道光标所在位置的行号?

Given a multi-line EditText, how can I know the line number at cursor position?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

儭儭莪哋寶赑 2024-10-06 13:38:11

像这样的东西
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

水晶透心 2024-10-06 13:38:11

您的问题有一个解决方法。

这个问题可能会给您一些进一步的见解:如何获取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?

伴我老 2024-10-06 13:38:11

获取控件的文本并将其放入字符串中。循环字符串直到光标位置的索引并计算换行符的数量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文