2. Tailored to Your Needs
Android 1.1 and earlier offered many attributes on EditText widgets to control their style of input, such as android:password
to indicate a field should be for password entry (shrouding the
password keystrokes from prying eyes). Starting in Android 1.5, with the
IMF, many of these attributes have been combined into a single android:inputType attribute.
The android:inputType attribute takes a class plus modifiers, in a pipe-delimited list (where |
is the pipe character). The class generally describes what the user is
allowed to input, and this determines the basic set of keys available on
the soft keyboard. The available classes are as follows:
text (the default)
number
phone
datetime
date
time
Many of these classes
offer one or more modifiers to further refine what the user will be
allowed to enter. To get a better understanding of how these modifiers
work, take a look at the res/layout/main.xml file from the InputMethod/IMEDemo1 project:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:text="No special rules:"
/>
<EditText
/>
</TableRow>
<TableRow>
<TextView
android:text="Email address:"
/>
<EditText
android:inputType="text|textEmailAddress"
/>
</TableRow>
<TableRow>
<TextView
android:text="Signed decimal number:"
/>
<EditText
android:inputType="number|numberSigned|numberDecimal"
/>
</TableRow>
<TableRow>
<TextView
android:text="Date:"
/>
<EditText
android:inputType="date"
/>
</TableRow>
<TableRow>
<TextView
android:text="Multi-line text:"
/>
<EditText
android:inputType="text|textMultiLine|textAutoCorrect"
android:minLines="3"
android:gravity="top"
/>
</TableRow>
</TableLayout>
This shows a TableLayout containing five rows, each demonstrating a slightly different flavor of EditText:
The first row has no attributes at all on the EditText, meaning you get a plain text-entry field.
The second row has android:inputType = "text|textEmailAddress", meaning it is a text-entry field that specifically seeks an e-mail address.
The third row allows for signed decimal numeric input, via android:inputType = "number|numberSigned|numberDecimal".
The fourth row is set up to allow for data entry of a date (android:inputType = "date").
The last allows for multiline input with autocorrection of probable spelling errors (android:inputType = "text|textMultiLine|textAutoCorrect").
The class and modifiers tailor the keyboard. So, a plain text-entry field results in a plain soft keyboard, as shown in Figure 2.
An e-mail address field might put the @ symbol on the soft keyboard, at the cost of a smaller spacebar, as shown in Figure 3.
Note, though, that this behavior is specific to the IME. Some editors might put the @ symbol on the primary keyboard for an e-mail field. Some might put a .com
button on the primary keyboard. Some might not react at all. It is up
to the implementation of the IME—all you can do is supply the hint.
Number and date fields
restrict the keys to numeric keys, plus a set of symbols that may or may
not be valid on a given field, as shown in Figure 4.
These are just a few examples of the possible IMEs. By choosing the appropriate android:inputType, you can give users a soft keyboard that best suits the type of data they should be entering.