Text View and Edit Text

A TextView, as shown in the line "This is some text" in Figure 11-1, is just what you'd expect: a place to display a text string. The vanilla TextView is for display only, whereas EditText is a predefined subclass of TextView that includes rich editing capabilities.

Each TextView has the attributes you'd expect of such a component: you can change its height, width, font, text color, background color, and so forth. TextViews also have some useful unique attributes:

autoLink

If set (true), finds URLs in the displayed text and automatically converts them to clickable links. autoText

If set (true), finds and corrects simple spelling errors in the text. editable

If set (true), indicates that the program has defined an input method to receive input text (default is false for TextView, and true for EditText). inputMethod

Identifies the input method (EditText defines one for generic text).

Example 11-1 shows how to use a TextView and an EditText with Buttons. (Buttons are covered in the next section.) It also shows the XML layout file (main.xml), which uses pretty standard and recommended layout parameters.

Example 11-1. Layout file for TextView and EditView example <?xml version="l.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"

<TextView android:id="@+id/txtDemo" android:layout_width="fill_parent" android:layout_height="wrap_content" />

<EditText android:id="@+id/eTxtDemo" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnDone"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Log it" />

</LinearLayout>

Example 11-2 contains the accompanying Java source (TextViewDemo.java).

Example 11-2. Java for TextView and EditView: TextViewDemo.java package com.oreilly.demo;

import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button;

import android.widget.EditText; import android.widget.TextView;

public class TextViewDemo extends Activity { private static TextView txt1; private static EditText etxt1; private static Button btn1;

// Create a button click listener for the Done button.

private final Button.OnClickListener btnDoneOnClick = new Button.OnClickListener() { public void onClick(View v) { String input = etxt1.getText().toString(); //Log the input string Log.v("TextViewDemo", input); etxt1.setText("");

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

//Get pointers to the Views defined in main.xml txt1 = (TextView) findViewById(R.id.txtDemo);0 etxt1 = (EditText) findViewById(R.id.eTxtDemo); btn1 = (Button) findViewById(R.id.btnDone);

//Set the string displayed in TextView1 txt1.setText("This is some text.");

//Set the OnClickListener for the Done button btn1.setOnClickListener(btnDoneOnClick);

Here are some of the highlights of the code:

O Defines a ClickListener that we'll attach to the "Log it" Button.

© Because onCreate is executed just once, as soon as Android instantiates this View, we put all the configuration we need here.

© Loads the XML layout file for the application by setting the ContentView to main.xml.

O Finds the Views that are defined in main.xml.

© Puts an initial string into the TextView. (We also could have done this in the XML file, as was done in the MicroJobs application in "Initialization in Micro-Jobs.java" on page 46.)

© Connects the Button with the ClickListener.

Now the user can enter and edit text in the EditText, and when he clicks on "Log it", the OnClickListener is called and the text is written to the logcat log. The string in the EditText is cleared out, and the widget is ready for another entry.

0 0

Post a comment