Android has enabled its IMF (Input Method Framework) Since SDK 1.5, allowing us to develop our own input methods. The best reference for the development input method is the sample-softkeyboard provided by Android. Although this example only contains English and numbers, it is still complete and clear, it is of great help for us to start Android development.
What is IMF:
The Input Method Framework (IMF) is used to support various input methods, such as soft keyboard input, hand input, and physical keyboard input (soft keyboard, hand-writing recognizers, and hard keyboard translators ). We focus on soft keyboard input.
IMF structure:
An IMF structure contains three main parts:
Input Method Manager: Manages interactions between different parts. It is a client API that exists in the context of each application and is used to communicate and manage global system services for interaction between all processes.
Input Method (IME): implements an independent Interaction module that allows users to generate texts. The system binds the current input method. Create and generate the input method, and decide when to hide or display its UI. Only one IME can run at a time.
Client application: controls the input focus and IME status through the Input Method Manager. Only one client can use IME at a time.
Create an input method:
(1) InAndroidManifest.xmlThe input method must be declared as a service, and the intent filter (associated meta data) must be added ):
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.fastinput"> <application android:label="@string/app_label"> <!-- Declares the input method service --> <service android:name="FastInputIME" android:label="@string/fast_input_label" android:permission="android.permission.BIND_INPUT_METHOD"> <intent-filter> <action android:name="android.view.InputMethod" /> </intent-filter> <meta-data android:name="android.view.im" android:resource="@xml/method" /> </service> <!-- Optional activities. A good idea to have some user settings. --> <activity android:name="FastInputIMESettings" android:label="@string/fast_input_settings"> <intent-filter> <action android:name="android.intent.action.MAIN"/> </intent-filter> </activity> </application></manifest>
(2) OneInputMethodServiceSuch:
(3) Two important views:
The input view and the candidates view ):
The input view is the main place of interaction with users: buttons, drawing, or other methods. The general implementation is to simply use a view to process all the work, and return a new instance when inputmethodservice. oncreateinputview () is called. The system calls onevaluateinputviewshow () to test whether the input view needs to be displayed. It is implemented by the system based on the current context. When the input method status changes, you need to call updateinputviewshown () to re-estimate it.
The candidates view: After a user inputs some characters, the input method may be provided to the user with a list of available candidate words. The management of this view is not the same as that of the input view, because it is very short-lived and will only be displayed when there are candidate words. Call inputmethodservice. oncreatecandidatesview () to return null by default. You can use setcandidatesviewshow () to set whether to display this view. It is precisely because of the frequency of display that it will not be destroyed and will not change the view of the current application.
(4) design different input types for the input method:
You can design different input types based on your purposes. For example, latinime provides the following two input interfaces:
(5)EditorInfoObject
EditorInfoThe object provides values of different input types and text fields.
(EditorInfo.inputType & EditorInfo.TYPE_CLASS_MASK) Can provide different values, as follows:
Number area: type_class_number
Time: type_class_datetime
Tel: type_class_phone
Text: type_class_text
Others include password input;
(6) Send text to the application (Sending text to the application)
There are two ways to submit text to the application:
1. Send individual key events
2. Edit the text around the cursor in the application's text field
× Send a key event. You can use the keyevent object and callInputConnection.sendKeyEvent()Method:
InputConnection ic = getCurrentInputConnection();long eventTime = SystemClock.uptimeMillis();ic.sendKeyEvent(new KeyEvent(eventTime, eventTime,KeyEvent.ACTION_DOWN, keyEventCode, 0, 0, 0, 0,KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));ic.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime,KeyEvent.ACTION_UP, keyEventCode, 0, 0, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
You can also use this simple method (recommended method ):
InputMethodService.sendDownUpKeyEvents(keyEventCode);
× When editing text (when editing text in a text field) in a text field, Android. View. inputmethod. inputconnection provides the following useful methods:
getTextBeforeCursor()
getTextAfterCursor()
deleteSurroundingText()
commitText()
For example:
InputConnection ic = getCurrentInputConnection();ic.deleteSurroundingText(4, 0);ic.commitText("Hello", 1);ic.commitText("!", 1);(7) composing text before submission (composing text before committing)
When you enter words, Chinese characters, and other composite words, you can display the progress or candidate words in the text field for your choice.
InputConnection ic = getCurrentInputConnection();ic.setComposingText("Composi", 1);...ic.setComposingText("Composin", 1);...ic.commitText("Composing ", 1);
8) Respond to the physical keyboard input (intercepting hard key events)
To respond to physical keyboard input, rewriteInputMethodService.onKeyDown()AndInputMethodService.onKeyUp()Method. Call the super. onkey method.
For more information, see latinime source code.