Positioning cursor at the beginning of pasted text with InputConnection.commitText

The documentation for InputConnection.commitText(CharSequence text, int newCursorPosition) says that newCursorPosition means:

int: The new cursor position around the text in Java characters. If> 0, this is relative to the end of the text - 1; if <= 0, then this is relative to the beginning of the text. Thus, a value of 1 will always advance the cursor to a position after entering the full text. Note that this means that you cannot place the cursor in the text, because the editor can make changes to the text that you provide so that it cannot correctly indicate the places there.

In this example , if I enter two characters, then place the cursor between them, like this

enter image description here

and then enter another character, it doesn't matter if I set newCursorPosition to 0 or 1 . The cursor is always at the end of input. For example, a call

 inputConnection.commitText("aaa", 0); 

or

 inputConnection.commitText("aaa", 1); 

Both show the cursor as follows:

enter image description here

If I -1 s

 inputConnection.commitText("aaa", -1); 

I get it

enter image description here

Results 1 and -1 are expected in accordance with the documentation. Why doesn't 0 place the cursor at the beginning of the insert? I would expect 0 to look like this:

 inputConnection.commitText("aaa", 0); 

enter image description here

but this is not so. Why not?

+5
source share
1 answer

It looks like a code defect, but you are the judge.

See replaceText () in BaseInputConnection . I believe this is the code that places the cursor after insertion. ( replaceText() is called from commitText()) .

In the reference code a is the beginning of the selection. b is the end of the choice. Since there is no choice in the example, and the cursor is at index 1, a == b == 1 . In addition, new text (aaa) is not inserted (replacing the selection [a, b]) until the cursor is moved to the new one.

Selection.setSelection(content, newCursorPosition) sets the cursor position, so for 0 and 1 to create identical positioning in your example, I expect the derived value of newCursorPosition be the same for both inputs.

Using the cursor located between two 8 at position 1, consider the following code:

 if (newCursorPosition > 0) { newCursorPosition += b - 1; } else { newCursorPosition += a; } 

For input 1, newCursorPosition > 0, so newCursorPosition = newCursorPosition + 1 - 1 or 1.

For your input 0, newCursorPosition not 0, so newCursorPosition = newCursorPosition + a (0 + 1) or 1.

Since both inputs give the same value, I would expect Selection.setSelection(content, newCursorPosition) to get the results you see.

I did not execute the code in this place, but I believe that this is a problem. I followed the execution paths in BaseInputConnection for newCursorPosition = 0 and newCursorPosition = 1 on the Pixel Emulator with API 21 and what is described above fails.

+3
source

Source: https://habr.com/ru/post/1269954/


All Articles