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.

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.

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату
×