简体   繁体   中英

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

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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