Send text to ApplicationProgram
When you use your IME to input text, you can send the text to the application by sending a separate button event or editing the text near the cursor in the text field of the application. In both cases, you need to use an inputconnection object to send text. Call inputmethodservice. getcurrentinputconnection () to obtain the instance.
Edit text near the cursor
When processing existing text in the text field, there are some useful methods in the baseinputconnection class:
Gettextbeforecursor ()
Returns a charsequence string that contains all the required characters before the current position of the cursor.
Gettextaftercursor ()
Returns a charsequence string that contains all the required characters after the current position of the cursor.
Deletesurroundingtext ()
Delete the specified number of characters selected for the current cursor position.
Committext ()
Submit a character string of the charsequence type to the text field and set a new cursor position.
For exampleCodeDemonstrate how to replace the text fell with hello! :
Inputconnection Ic = getcurrentinputconnection ();
IC. deletesurroundingtext (4, 0 );
IC. committext ("hello", 1 );
IC. committext ("! ", 1 );
Orchestration text before submission
If your IME text pre-selection or requires multiple steps to complete text orchestration, you can display the progress in the text field until the user submits the text, you can replace the input text with the complete text. When you pass the text to the setcomposingtext () method of the inputconnection class, you can add a "range" to perform special processing on the text.
The following code displays the characters in a text field:
Inputconnection Ic = getcurrentinputconnection ();
IC. setcomposingtext ("composi", 1 );
...
IC. setcomposingtext ("composin", 1 );
...
IC. committext ("composing", 1 );
The following shows how the text is displayed to the user:
Figure 3. Text orchestration before submission.