Create a new input method in Android (Creating an input method ))

Source: Internet
Author: User

Translated from [url] http://android-developers.blogspot.com/2009/04/creating-input-method.html#/url]
If you need to reprint by fly fire, please indicate the source [url] www.yddev.com [/url]

I.
1. To create a new input method, you must inherit Android. inputmethodservice. inputmethodservice. This class provides an input method.
For example, you can refer to the softkeyboard code in the SDK.

2. The input method will be packaged into an APK like other applications or services. In androidmanifest. XML, declare it as
Service. [Code] <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> [/Code] 3. The service lifecycle of the input method is as follows:
[Img] https://docs.google.com/a/google.com/File? Id = ad9xdxhtb4_043qn7phd_ B [/img]

Ii. interface elements of the Input Method

The input method has two main interface elements: inputview and candidates view.
Inputview: the place where the user inputs the text. When the input method is displayed, inputmethodservice. oncreateinputview () is called ()
In this function, create and return the input view you want to display in the input method window.
Candidates view: used to provide input selection. It is created in the inputmethodservice. oncreatecandidatesview () function. The default value is
Is empty.

3. Design different input types
The text box of a program may have different input types, such as characters, numbers, URLs, and email addresses. When you implement an input method
You need to know the differences between different input methods, and the input method will not be automatically switched based on different input types, so your input method must support all
Input type. Verification of input data is handled by the application.

For example, in Android, a Latin input method provides the character and number input interface:
[Img] https://docs.google.com/a/google.com/File? Id = ddvqp2ns_18g2nm5jhb_ B [/img]

[Img] https://docs.google.com/a/google.com/File? Id = ddvqp2ns_17crk39fd9_ B [/img]

When inputmethodservice. onstartinputview () is called, an editorinfo object is passed to determine the input type.
For example, you can use (editorinfo. inputtype & editorinfo. type_class_mask) to determine which of the following types is used:
Type_class_number
Type_class_datetime
Type_class_phone
Type_class_text

Password Input: do not display the password on your interface. Do not save the password unless prompted.

4. Send the input text to the application
1. You can send a key event to implement [Code] 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); [/Code] or
[Code] inputmethodservice. senddownupkeyevents (keyeventcode); [/Code] for some
The first method is used in the input mode, because some buttons may be filtered.

2. Edit the input text by using the following methods. [Code] gettextbeforecursor ()
Gettextaftercursor ()
Deletesurroundingtext ()
Committext () [/Code]. For example, if you want to replace a text starting with fell with hello! [Code] inputconnection Ic = getcurrentinputconnection ();
IC. deletesurroundingtext (4, 0 );
IC. committext ("hello", 1 );
IC. committext ("! ", 1); [/Code] 5. Lenovo Input
If you need to predict the input text dynamically during Lenovo input or input, you can refer to the following code: [Code] inputconnection Ic = getcurrentinputconnection ();
IC. setcomposingtext ("composi", 1 );
...
IC. setcomposingtext ("composin", 1 );
...
IC. committext ("composing", 1); [/Code] [img] https://docs.google.com/a/google.com/File? Id = ddvqp2ns_0g2z84dg2_ B [/img]

[Img] https://docs.google.com/a/google.com/File? Id = ddvqp2ns_1jnw696d3_ B [/img]

[Img] https://docs.google.com/a/google.com/File? Id = ddvqp2ns_2crrwtbf7_ B [/img]

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.

8. Example:
Refer to the latinime example:
[Url = http://android.git.kernel.org /? P = platform/packages/inputmethods/latinime. Git; A = tree] http://android.git.kernel.org /? P = platform/packages/inputmethods/latinime. Git; A = tree [/url]
Or the 1.5 SDK also provides a softkeyboard example.

Terry
Posted on

Is this the core input of version 1.5? Pretty good

Fly_fire
Posted on

[Quote] Is this core input of version 1.5? Pretty good
[Size = 2] [color = #999999] Terry posted on [/color]
[Url = http://yddev.com/bbs/redirect.php? Goto = findpost & pid = 96 & ptid = 63] [img] average

Yes, this is the new input method created based on the 1.5 Input Method Framework ..

Yantao821015
Posted at, September 22

How can I set the current input method through the API?
I read the setting code. He uses
Settings. Secure. putstring (getcontentresolver (),
Settings. Secure. enabled_input_methods, builder. tostring ());
However, the SDK documentation clearly states that this setting can only be read and cannot be written. Depressed...
Give me some advice

Http://www.yddev.com/bbs/viewthread.php? Tid = 63

 

 

 

 

Softkeyboard

Summary:

Softkeyboard. Java

Main variables:

Stringbuilder mcomposing is used to store the string submitted to the linked Application

Main interface functions:

Public void onkey (INT primarycode, int [] keycodes ){
If (iswordseparator (primarycode) {// determines whether it is a specified symbol (space, semicolon, colon, etc ). The specified symbol is in string. xml.
// Handle Separator
If (mcomposing. Length ()> 0) {// is there any input before this symbol is entered?
Committyped (getcurrentinputconnection (); // submit the previous text to the Connected Application and clear the input state.
}
SendKey (primarycode); // execute the operation of the button itself (enter a space, colon, etc)
Updateshiftkeystate (getcurrentinputeditorinfo ());
} Else if (primarycode = keyboard. keycode_delete ){
Handlebackspace ();
} Else if (primarycode = keyboard. keycode_shift ){
Handleshift ();
} Else if (primarycode = keyboard. keycode_cancel ){
Handleclose (); // close the keyboard
Return;
} Else if (primarycode = latinkeyboardview. keycode_options ){
// Show a menu or somethin'
} Else if (primarycode = keyboard. keycode_mode_change
& Minputview! = NULL) {// switch the input sending status
Keyboard current = minputview. getkeyboard ();
If (current = msymbolskeyboard | current = msymbolsshiftedkeyboard ){
Current = mpinyinkeyboard; // mqwertykeyboard;
} Else if (current = mpinyinkeyboard ){
Current = mqwertykeyboard ;//
} Else {
Current = msymbolskeyboard;
}
Minputview. setkeyboard (current );
If (current = msymbolskeyboard ){
Current. setshifted (false );
}
} Else {
Handlecharacter (primarycode, keycodes); // enter common characters or symbols
}
}

Private void handlecharacter (INT primarycode, int [] keycodes ){
If (isinputviewshown () {// whether the soft keyboard is enabled
If (minputview. isshifted () {// check the keyboard status
Primarycode = character. touppercase (primarycode); // converts the case sensitivity
}
}
If (isalphabet (primarycode) & mpredictionon) {// use charactor. isletter () to determine whether it is a character and whether it has been input
Mcomposing. append (char) primarycode); // append a character to the end
Getcurrentinputconnection (). setcomposingtext (mcomposing, mcomposing. Length (); // sets the display of the current input text
Updateshiftkeystate (getcurrentinputeditorinfo ());
Updatecandidates ();
} Else {
Getcurrentinputconnection (). committext (
String. valueof (char) primarycode), 1); // directly submit the currently entered character to the link application
}
}

Http://vivien0515.blog.163.com/blog/static/11096733200921753459780/

 

 

 

Http://blog.csdn.net/Fendy_Dai/archive/2010/06/08/5656081.aspx

Http://blog.163.com/quickpy@yeah/

Http://www.javaeye.com/topic/574406

 

Http://www.gsmcdma .com/a/ruanjiankaifa/yingyongkaifa/2010/0515/3.html

Http://www.yddev.com/bbs/thread-63-1-1.htmlchinese version http://www.heiher.info/855.html http://news.wangmeng.cn/detailNews/2616-android-development-of-a-simple-description-of-the-input

 

Http://www.devdiv.net/viewthread-26807

Http://www.docin.com/p-47951191.html #

Http://a.77mi.com/Thread/view/cps-3/id-4646

 

 

Http://www.coolapk.com/docs/reference/android/inputmethodservice/InputMethodService.html

 

 

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.