如何在Android中的软键盘(不是EditText或TextView!!!)中设置光标位置?

发布于 2024-12-20 09:27:36 字数 469 浏览 6 评论 0原文

以下代码仅适用于 1000 个字符。如果超过 1000 个字符,光标将跳回 999 个字符末尾并重新开始工作。

这是因为 getTextBeforeCursor(n,flag) 只能提供当前光标位置之前的 n 个字符。然而,在EditText中,我们可以使用getText()方法获取所有文本,并且还可以获得长度。但是如何获取撰写文本区域中整个文本的长度,以便我可以将光标位置设置到我想要的任何位置。

InputConnection ic = getCurrentInputConnection();
ic = getCurrentInputConnection();

String str = ic.getTextBeforeCursor(1000,0).toString();
ic.setSelection(str.length()-1,str.length()-1);

The following code works for only 1000 characters. If there are more than 1000 characters, the cursor will jump back to the end of 999 characters and start working again.

This is because getTextBeforeCursor(n,flag) can offer only the n number of characters before the current cursor position. In EditText, however, we can get all the text by using getText() method and we can get the length as well. But how to get length of the entire text in the composing text area so that I can set the cursor position to anywhere I want.

InputConnection ic = getCurrentInputConnection();
ic = getCurrentInputConnection();

String str = ic.getTextBeforeCursor(1000,0).toString();
ic.setSelection(str.length()-1,str.length()-1);

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

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

发布评论

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

评论(1

枕花眠 2024-12-27 09:27:36

这很简单。我在做软件时刚刚得到解决方案
键盘。

CharSequence textBeforeCursor = getCurrentInputConnection().getTextBeforeCursor(1000, 0);

CharSequence textAfterCursor = getCurrentInputConnection().getTextAfterCursor(1000, 0);

然后只需计算并添加上述两个值的长度即可
可以获得撰写文本区域的长度。

如果有任何问题请告诉我。

It's very simple. I just got solution when I am working on soft
keyboard.

CharSequence textBeforeCursor = getCurrentInputConnection().getTextBeforeCursor(1000, 0);

CharSequence textAfterCursor = getCurrentInputConnection().getTextAfterCursor(1000, 0);

then by just calculating and adding of lengths of above two values you
can get length of composing text area.

If any problems just inform me.

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