Fields of Green Or Other Colors

Along with buttons and labels, fields are the third "anchor" of most GUI toolkits. In Android, they are implemented via the EditText widget, which is a subclass of the TextView used for labels.

Along with the standard TextView properties (e.g., android:textStyle), EditText has many others that will be useful for you in constructing fields, including:

• android:autoText, to control if the field should provide automatic spelling assistance

• android:capitalize, to control if the field should automatically capitalize the first letter of entered text (e.g., first name, city)

• android:digits, to configure the field to accept only certain digits

• android:singleLine, to control if the field is for single-line input or multiple-line input (e.g., does <Enter> move you to the next widget or add a newline?)

Beyond those, you can configure fields to use specialized input methods, such as android:numeric for numeric-only input, android:password for shrouded password input, and android:phoneNumber for entering in phone numbers. If you want to create your own input method scheme (e.g., postal codes, Social Security numbers), you need to create your own implementation of the InputMethod interface, then configure the field to use it via android: inputMethod.

For example, from the Basic/Field project, here is an XML layout file showing an EditText: <?xml version="1.0" encoding="utf-8"?>

<EditText xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/field" android:layout width="fill parent" android:layout height="fill parent" android:singleLine="false" />

Note that android:singleLine is false, so users will be able to enter in several lines of text. For this project, the FieldDemo.java file populates the input field with some prose:

package com.commonsware.android.basic;

import android.app.Activity; import android.os.Bundle; import android.widget.EditText;

public class FieldDemo extends Activity { @Override public void onCreate(Bundle icicle) { super. onCreate(icicle); setContentView(R.layout.main);

EditText fld=(EditText)findViewById(R.id.field); fld.setText("Licensed under the Apache License, Version 2.0 " + "(the \"License\"); you may not use this file " + "except in compliance with the License. You may " + "obtain a copy of the License at " + "http://www.apache.org/licenses/LICENSE-2.0");

The result, once built and installed into the emulator, is shown in Figure 6-3.

Figure 6-3. The FieldDemo sample application

■Note Android's emulator only allows one application in the launcher per unique Java package. Since all the demos in this chapter share the com.commonsware.android.basic package, you will only see one of these demos in your emulator's launcher at any one time.

Another flavor of field is one that offers auto-completion, to help users supply a value without typing in the whole text. That is provided in Android as the AutoCompleteTextView widget and is discussed in Chapter 8.

0 0

Post a comment