简体   繁体   English

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

[英]How to set cursor position in softkeyboard(not in EditText or TextView!!!) in Android?

The following code works for only 1000 characters. 以下代码仅适用于1000个字符。 If there are more than 1000 characters, the cursor will jump back to the end of 999 characters and start working again. 如果超过1000个字符,光标将跳回到999个字符的末尾并重新开始工作。

This is because getTextBeforeCursor(n,flag) can offer only the n number of characters before the current cursor position. 这是因为getTextBeforeCursor(n,flag)只能在当前光标位置之前提供n个字符。 In EditText, however, we can get all the text by using getText() method and we can get the length as well. 但是,在EditText中,我们可以使用getText()方法获取所有文本,并且还可以获取长度。 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);

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. 如果有任何问题,请通知我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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