Android Official Development Document Training Series Course Chinese version: keyboard input processing the type of the specified input

Source: Internet
Author: User

Original address: http://android.xsoftlab.net/training/keyboard-input/index.html

Introduction

When the text box receives the focus, the Android system displays a soft keyboard on the screen. To provide the best user experience, you can specify the characteristics of the relevant input type and how the input method should be presented.

In addition to the soft keyboard on the screen, Android also supports the physical keyboard, so how the app interacts with the various types of keyboards becomes important.

Specify the type of input

Each text box must have only one input type, such as an e-mail address, a phone number, or regular text. Therefore, it becomes important to specify the input type for each text box so that the correct input method is displayed.

You can specify, for example, the spelling suggestions provided by the input method, the capitalization of the initials, and the behavior of the lower-right button in the IME (done or next). This lesson mainly describes how to specify these attributes.

Specify keyboard type

You should always declare the input type for the text box, and you can add the input type to the text box by using the Android:inputtype property.

For example, if you want the input type of the text box to be a phone number, you can use "phone":

<EditText    android:id="@+id/phone"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:hint="@string/phone_hint"    android:inputType="phone" />

Or, if the text box is primarily used to enter a password, you can use "Textpassword" to hide the user's input text:

<EditText    android:id="@+id/password"    android:hint="@string/password_hint"    android:inputType="textPassword"    ... />    

Android:inputtype contains a number of specified input types, and some values can be used in combination.

Turn on spell checking and other features

The Android:inputtype property allows you to specify multiple behaviors for the input type. More importantly, if the focus of the text box is on the underlying text input (such as a text message), you should use "Textautocorrect" to turn on spell checking.

You can also specify many different behaviors and input types for the Android:inputtype property. For example, the following example shows how to turn on the initial capitalization as well as the spell check feature:

<EditText    android:id="@+id/message"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:inputType=        "textCapSentences|textAutoCorrect"    ... />
Specify the behavior of the Input Method button

Most input methods provide a user function button in the lower right corner, which is extremely appropriate for the current text box. By default, the system uses this button to implement Next or done functions. Unless your text box allows multiple lines to appear (such as using android:inputtype= "Textmultiline"). In this case, the function button is a carriage return button. However, you can specify some special features that are more consistent with your text box, such as Send or Go.

To specify the function buttons for the keyboard, you need to use the attribute android:imeoptions and need to perform values such as "Actionsend" or "Actionsearch":

<EditText    android:id="@+id/search"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:hint="@string/search_hint"    android:inputType="text"    android:imeOptions="actionSend" />

Next, you can listen to the down event of the function button via Textview.oneditoractionlistener and need to respond to the correct IME function ID within the listener, which is defined with the editorinfo, such as the following using Ime_action _send:

EditText editText = (EditText) findViewById(R.id.search);editText.setOnEditorActionListener(new OnEditorActionListener() {    @Override    publicbooleanonEditorActionint actionId, KeyEvent event) {        booleanfalse;        if (actionId == EditorInfo.IME_ACTION_SEND) {            sendMessage();            true;        }        return handled;    }});

Android Official Development Document Training Series Course Chinese version: keyboard input processing the type of the specified input

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.