Выбрать главу

 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.

Just Another Box to Check

The classic checkbox has two states: checked and unchecked. Clicking the checkbox toggles between those states to indicate a choice (e.g., “Add rush delivery to my order”).

In Android, there is a CheckBox widget to meet this need. It has TextView as an ancestor, so you can use TextView properties like android:textColor to format the widget.

Within Java, you can invoke:

• isChecked() to determine if the checkbox has been checked

• setChecked() to force the checkbox into a checked or unchecked state

• toggle() to toggle the checkbox as if the user checked it

Also, you can register a listener object (in this case, an instance of OnCheckedChangeListener) to be notified when the state of the checkbox changes.

For example, from the Basic/CheckBox project, here is a simple checkbox layout:

<?xml version="1.0" encoding="utf-8"?>

<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"

 android:id="@+id/check"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="This checkbox is: unchecked" />

The corresponding CheckBoxDemo.java retrieves and configures the behavior of the checkbox:

public class CheckBoxDemo extends Activity

 implements CompoundButton.OnCheckedChangeListener {

 CheckBox cb;

 @Override

 public void onCreate(Bundle icicle) {

  super.onCreate(icicle);

  setContentView(R.layout.main);

  cb=(CheckBox)findViewById(R.id.check);

  cb.setOnCheckedChangeListener(this);

 }

 public void onCheckedChanged(CompoundButton buttonView,

  boolean isChecked) {

  if (isChecked) {

   cb.setText("This checkbox is: checked");

  } else {

   cb.setText("This checkbox is: unchecked");

  }

 }

}

Note that the activity serves as its own listener for checkbox state changes since it implements the OnCheckedChangeListener interface (via cb.setOnCheckedChangeListener(this)). The callback for the listener is onCheckedChanged(), which receives the checkbox whose state has changed and what the new state is. In this case, we update the text of the checkbox to reflect what the actual box contains.

The result? Clicking the checkbox immediately updates its text, as you can see in Figures 6-4 and 6-5.

Figure 6-4. The CheckBoxDemo sample application, with the checkbox unchecked

Figure 6-5. The same application, now with the checkbox checked

Turn the Radio Up

As with other implementations of radio buttons in other toolkits, Android’s radio buttons are two-state, like checkboxes, but can be grouped such that only one radio button in the group can be checked at any time.

Like CheckBox, RadioButton inherits from CompoundButton, which in turn inherits from TextView. Hence, all the standard TextView properties for font face, style, color, etc., are available for controlling the look of radio buttons. Similarly, you can call isChecked() on a RadioButton to see if it is selected, toggle() to select it, and so on, like you can with a CheckBox.

Most times, you will want to put your RadioButton widgets inside of a RadioGroup. The RadioGroup indicates a set of radio buttons whose state is tied, meaning only one button out of the group can be selected at any time. If you assign an android:id to your RadioGroup in your XML layout, you can access the group from your Java code and invoke:

• check() to check a specific radio button via its ID (e.g., group.check(R.id.radio1))

• clearCheck() to clear all radio buttons, so none in the group are checked

• getCheckedRadioButtonId() to get the ID of the currently-checked radio button (or -1 if none are checked)

For example, from the Basic/RadioButton sample application, here is an XML layout showing a RadioGroup wrapping a set of RadioButton widgets:

<?xml version="1.0" encoding="utf-8"?>

<RadioGroup

 xmlns:android="http://schemas.android.com/apk/res/android"