Input Method API
Classes with IME can be found in the Android. inputmethodservice and Android. View. inputmethod packages. The keyevent class is critical for handling keyboard characters.
The core part of IME is the service component, which inherits the inputmethodservice class. In addition to implementing the standard service life cycle, this class also provides a callback method for the ime UI to process user input and send the text to the text domain with the current input focus. By default, the inputmethodservice class provides most implementations for managing the ime status and visibility, and communicates with the current input domain.
The following classes are also important:
Baseinputconnection
This class defines the communication channel between the inputmethod class and the application that receives its input. Use this channel to read text within the cursor range, submit the text to the text box, and send the original button event to the application. The application should inherit this class, instead of implementing the basic interface: inputconnection.
Keyboardview
This class is a subclass of the View class. It presents a soft keyboard and responds to user input events. The keyboard layout is specified by a keyboard instance. You can define the layout in an XML file.
UI for designing input methods
For IME, there are two main visual elements: Input view and candidates view. You only need to implement the elements related to the input method you designed.
Input View
The input view is the UI for users to enter text by clicking the keyboard, handwriting, or using gestures. When the IME is displayed for the first time, the system will call its oncreateinputview () callback method. In the implementation of this method, you need to create the layout of the IME window you want and return the layout to the system. The following is an implementation example of the oncreateinputview () method:
@Override
Public View oncreateinputview (){
Mykeyboardview inputview =
(Mykeyboardview) getlayoutinflater (). Inflate (R. layout. Input, null );
Inputview. setonkeyboardactionlistener (this); inputview. setkeyboard (mlatinkeyboard );
Return minputview;
}
In this example, mykeyboardview is a custom keyboardview class implementation instance that displays a soft keyboard. If you are creating a traditional QWERTY keypad, see the example program of the keypad: Sample
App. How it inherits the keyboardview class.
Candidates View
Candidates view is the place where the user selects candidate words. During the ime lifecycle, When you prepare to display the candidate view, the system will call its oncreatecandidatesview () callback method. In the implementation of this method, it will return the layout of a suggested candidate word. If there is no content to be displayed, it will return NULL (if you provide a candidate suggestion, then you do not need to implement this method, because the null response is the default action ).
Factors to consider in uidesign
Next, we will introduce some of the following considerations when designing the ime UI:
1. handle multiple screen sizes
Your ime ui must be scaled based on different screen sizes, and you must also handle the differences between landscape and landscape screens. In non-full-screen IME mode, to leave enough space for your application to display text fields and corresponding content, the screen space occupied by IME should not exceed half a screen. In full-screen IME mode, this is not a problem,
2. process different input types
Android text fields allow you to set special input types, such as text, numbers, URLs, email addresses, and search strings. When you implement a new IME, you need to check the input type of each input field and provide the corresponding UI interface. In this way, you do not have to check whether the text you type is a valid input type, because the text field of the application is responsible for this.
For example, the following Latin IME interface provides text and phone number input:
Figure 2. Latin IME input type
When an input field receives the focus and starts IME, the system calls the onstartinputview () callback method. The system passes an editorinfo object to this method, it contains details of the input type and other text domain attributes. In this object, the inputtype field contains the input type of the text field.
The inputtype field is an integer field that contains the bit modes set for various input types. To check the input type of the text field, use the type_mask_class constant to perform mask operations, such:
Inputtype & inputtype. type_mask_class
The bit mode of the input type can be one of the following values:
TYPE_CLASS_NUMBER
The text field of the received number.
TYPE_CLASS_DATETIME
The text field of the Receiving date and time.
TYPE_CLASS_PHONE
Text Field of the received phone number
TYPE_CLASS_TEXT
The text field that receives all supported characters.
For more information about these constants, see the inputtype document.
The inputtype field can contain other characters that indicate changes in the text field type, such:
TYPE_TEXT_VARIATION_PASSWORD
It is a variant of the type_class_text type and is used to enter the password. The input method uses modifiers to represent the display of actual text.
TYPE_TEXT_VARIATION_URI
It is a variant of the type_class_text type, used to enter the URL and other types of URIs.
TYPE_TEXT_FLAG_AUTO_COMPLETE
It is a variant of the type_class_text type and is used to input automatically completed text that an application obtains from a dictionary, search, or other convenient place.
Warning in your own IME, when you send text to the Password text field, make sure that the text is correctly processed, because the passwords in the UI of the input view and candidates view must be hidden. Remember not to save the password on the device.