Working with Edit Text Controls

The EditText control, which is derived from the TextView control, is used to collect textual input from the user. Figure 10.3 shows a simple EditText control.

FIGURE 10.3

An EditText control for text input.

This is an input field..

Configuring EditText Controls

All the typical attributes of a TextView control (for example, textColor, textSize) are available to EditText controls. The following are some EditText attributes that are commonly used for the settings screen:

► inputType—Instructs the Android system about how to help the user fill in the text. For example, you set the inputType attribute of the EditText control for the Email field to textEmailAddress, which instructs the Android system to use the email-oriented soft keyboard (with the @ sign). The inputType value called textPassword automatically masks the user's password as it is typed.

► minLines and maxLines—Restrict the number of lines of text allowed in the control.

► maxLength—Restricts the number of characters of text allowed in the control. For example, you can limit the number of characters allowed in the Nickname setting by setting the maxLength attribute of the Nickname setting's EditText control to 20.

Handling Text Input

As with a TextView control, you can access the text stored in an EditText control by using the getText() and setText() methods. For example, to extract the string typed into the EditText control called EditText_Nickname, you use the getText() method as follows:

EditText nicknameText = (EditText) findViewById(R.id.EditText_Nickname); String strNicknameToSave = nicknameText.getText().toString();

The getText() method returns an Editable object, but in this case, you simply want its String value equivalent.

Committing EditText Input

To handle EditText input to a form, you need to determine when new text has been entered. To do this, you can listen for EditText key events and commit the text entered when certain keys are pressed. For example, to listen for the Enter key during input of the Nickname setting, you would register View.OnKeyListener by using the setOnKeyListener() method of the EditText control, as follows:

final EditText nicknameText =

(EditText) findViewById(R.id.EditText_Nickname); nicknameText.setOnKeyListener(new View.OnKeyListener() {

public boolean onKey(View v, int keyCode, KeyEvent event) { if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {

String strNicknameToSave = nicknameText.getText().toString(); // TODO: Save Nickname setting (strNicknameToSave) return true;

return false;

Listening for EditText Keystrokes

Eventually, you will design a Password dialog. Say that you want to match the text strings within two EditText password fields, called EditText_Pwd1 and EditText_Pwd2, while the user is typing. A third TextView control, called TextView_PwdProblem, provides feedback about whether the passwords match.

First, you need to retrieve each of the controls:

final EditText pi = (EditText) findViewById(R.id.EditText_Pwd1); final EditText p2 = (EditText) findViewById(R.id.EditText_Pwd2); final TextView error = (TextView) findViewById(R.id.TextView_PwdProblem);

Next, you add TextWatcher to the second EditText control, using the addTextChangedListener() method, like this:

p2.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) {

String strPassl = p1.getText().toString(); String strPass2 = p2.getText().toString(); if (strPass1.equals(strPass2)) {

error.setText(R.string.settings_pwd_equal); } else {

error.setText(R.string.settings_pwd_not_equal);

// Other required overrides do nothing

The user can type the password into the EditText_Pwd1 EditText control normally. However, each time the user types a character into the EditText_Pwd2 control, you compare the text in both EditText controls and set the text of the TextView control called TextView_PwdProblem to reflect whether the text matches (see Figure 10.4).

FIGURE 10.4

A Password dialog with two EditText controls and a TextView control.

0 0

Post a comment