Developing an input method for the Android platform

Source: Internet
Author: User

Learning Goals:

    1. Implementing a new Input method

Purpose of Study:

    1. Mastering the Android IME framework

Learn to Harvest:

One of the new features of Android 1.5 is the Input method framework (Input Method FRAMEWORK,IMF), which appears to provide the possibility of birth without a physical keyboard. The IMF was designed to support different IMEs, including soft keyboard,hand-writing recognizes and hard keyboard translators. Here, we lock the focus on the soft keyboard.

New features for general application developers, it is important to ensure that the app works well with the IMF to provide an excellent user experience. The most important thing for an application to do is to use the new attribute Android:inputtype for each edittext, which actually replaces many existing attributes, including Android:password, Android:singleline, Android:numeric, Android:phonenumber, Android:capitalize, Android:autotext, android:editable. If you declare all two, the cupcake device will use the new Android:inputtype attribute and ignore the others.

The main API is Android.view.inputmethod.InputMethodManager, which you can get through the Context.getsystemservice method. It allows you to interact with the Global IME state, such as explicitly hiding or displaying the IME's input region.

New features for the system developers, it provides the development of a variety of language input method implementation portal, with it, the other language input method can be stationed in Android. Here's how to implement an input method.

To create an input method, you need to inherit android.inputmethodservice.InputMethodService. This class provides a basic implementation of the input method, including state management, control input methods visible, and current activity communication. Android provides two input methods and an example, the two input methods are pinyinime and Latinime input method, you can find in the source code Packages/inputmethods, the example is SoftKeyboard, can be in the SDK 1.5 under the platforms/android-1.5/samples found below. These three input methods provide the best reference for implementing the input method of the Android platform currently.

The input method is packaged in the same way as other applications, and in Androidmanifest.xml, the input method is declared as a service, with the appropriate intent filter and associated meta data attached. As shown below:

<service android:name=". Demoime "
Android:label="@string/ime_name"
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>

If the input method allows the user to adjust settings, you should also provide a setting activity. Don't forget to add the relevant attributes to the Input-method XML file, as shown below (this file is the @xml/method in the Meta-data just now):

<input-method xmlns:android="Http://schemas.android.com/apk/res/android"
android:settingsactivity="Com.demo.SettingsActivity"
android:isdefault="@bool/im_is_default" />

Input method on the UI display, there are two main visible elements, input view and candiate view. But this is not necessary, you can choose the elements you need according to the actual needs. Input View is where the user enters text from the keyboard, handwriting, or other means. Inputmethodservice.oncreateinputview () is called when the input method is first displayed. Candidates View is where the set of candidate words appears. It is optional and can be returned when Inputmethodservice.oncreatecandidatesview is called null,– this is its default behavior.

The text area of your app can have different input types, including text, numbers, URLs, email addresses, and searches, so you need to be wary of the different input types when you implement new input methods. The IME does not automatically switch between different input types, so you want to support all types in the IME. However, it is easy to point out that the IME is not responsible for validating the input content-because it is the responsibility of the application. Web Templates

When Inputmethodservice.onstartinputview () is called, it passes in a Editorinfo object that contains details about the input type and other properties of the text field. Editorinfo.inputtype and Editorinfo.type_class_mask can be a lot of values, including Type_class_number,type_class_datetime,type_class_phone , Type_class_text. More information can be found through Android.text.InputType.

There are two ways to send the resulting to the app, you can either transmit a single keystroke event, or you can edit the text near the cursor in the app's text box. Send a key event, you can simply construct the KeyEvent object, and call Inputconnection.sendkeyevent (), or more conveniently, using inputmethodservice.senddownupkeyevents ( Keyeventcode). http://www.huiyi8.com/moban/when editing a text box, Android.view.inputmethod.InputConnection has some useful methods, such as Gettextbeforecursor () Wait a minute.

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.