Android text and input-create input method (3)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.