Android-Input Method

Source: Internet
Author: User
To create an input method for the input text box or other views, you must inherit the inputmethodservice class. Inputmethodservice provides a lot of basic implementations of input methods, terms of Management Status, visibility of input methods, and communication with the currently visible activity.
A good starting point is the softkeyboard sample provided in the SDK. You can also modify the sample code to create your own input method.
The input method is packaged like an application or service. In the androidmanifest. xml file, the input method is declared as a service with some appropriate intent filters and related metadata: Metadata :-----------------------------------------------------------------------------------------------------
<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>
Bytes -----------------------------------------------------------------------------------------------------
If your input allows the user to adjust some settings, you should provide a setting page that can run on the setting application. This is optional. You can also choose to provide all user settings directly on the ime UI.

Lifecycle of an input method:

Visual elements:
The input method has two main visual elements: Input view and candidate view. If one of them is unrelated to your input method experience, you do not have to follow this style. Input view: you can press the button, hand-writing, or other gesture view. When inputmethodservice. oncreateinputview () is called, the input method is displayed immediately. Create and return the input method window view level you want to display.
Candidate view: potential, submitted user-selected, can be associated or not associated with your input method. The default behavior is when inputmethodservice is called. oncreatecandidatesview (), you can return null.
Different input types are designed: a text field can have different input types specified on them, such as free-form input methods, numbers, URLs, e-mail addresses, and searches. When you implement an input method, You Need To note different input types. The input method cannot automatically replace the input type, so you need to support all types on your ime. However, the input method is not responsible for verifying that the input is sent to the application, which is the responsibility of the application. For example, latinime provides different la s for text and phone number input on the Android platform:

Inputmethodservice. onstartinputview (), with an editorinfo containing details about other attributes of text fields of the input type and application, is called.
(Editorinfo. inputtype & editorinfo. type_class_mask) can be many different values, including:
Type_class_number
Type_class_datetime
Type_class_phone
Type_class_text
Editorinfo. inputtype can contain other masks, indicating class changes and other flags. For example, type_text_variation_password, type_text_variation_uri, or type_text_flag_auto_complete.

Password domain

Be sure not to display the password on your interface. Do not save the password in addition to reminders. Pass the input text to the application
There are two ways to send text to the application. You can send a separate button event or edit the text field cursor Text of the surrounding application.

To send a key event, you can simply construct the keyevent object and call inputconnection. sendkeyevent (). The following are some examples:
Bytes --------------------------------------------------------------------------------------------------
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 ));
Bytes -----------------------------------------------------------------------------------------------
Alternatively, use the following convenient methods:
Bytes -----------------------------------------------------------------------------------------------
Inputmethodservice. senddownupkeyevents (keyeventcode );
Bytes -----------------------------------------------------------------------------------------------
Note: It is recommended to use methods in certain fields such as telephone number fields that may apply each text filter after a key. The Return key and deletion key should also be used as raw materials for certain input types. For example, the application may be viewed as a specific key event to perform an action.
When editing the text of a text field, Android. View. inputmethod. inputconnection provides the following useful methods:
Bytes -----------------------------------------------------------------------------------------------
Gettextbeforecursor ()
Gettextaftercursor ()
Deletesurroundingtext ()
Committext ()
For example, assume that the text "fell" is the cursor on the left and you want to replace it with "Hello !" :
-----------------------------------------------------------------
1. inputconnection Ic = getcurrentinputconnection ();
2. IC. deletesurroundingtext (4, 0 );
3. IC. committext ("hello", 1 );
4. IC. committext ("! ", 1 );
Writing before submission
If your input method is used to make predictions of text objects or requires multiple steps to form a word or a font, you can display the text until the user's progress in the text field is broken, then you can replace the completed text parts. The text that is composed this time will be highlighted in the text field in some way, such as underline.
------------------------------------------------------
Inputconnection Ic = getcurrentinputconnection ();
IC. setcomposingtext ("composi", 1 );
....
IC. setcomposingtext ("composin", 1 );
....
IC. committext ("composing", 1 );
6. Intercept hardware button messages
Although the input method window does not have foucos, it receives the hardware key messages first. to process these hardware key messages, you only need
Override inputmethodservice. onkeydown () and inputmethodservice. onkeyup (). If you do not want to process a button, remember to call
Use super. onkey *.
7. other notes
1. provides a method for users to directly set input methods from the current input method.
2. A user can switch between different input methods.
3. Let the input method interface pop up as soon as possible. Resources or time-consuming operations can be loaded later.
4. When the input method window is hidden, it is best to release large memory allocation as soon as possible.
5. Ensure that the input method can contain the most common characters.
Bytes -----------------------------------------------------------------------------------------------
 
 
 
Bytes -----------------------------------------------------------------------------------------------------
 
 
Bytes -----------------------------------------------------------------------------------------------------

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.