如何在Android中的软键盘(不是EditText或TextView!!!)中设置光标位置?
以下代码仅适用于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)