Recipe Creating a Form

A form is a graphical layout with areas that can take text input or selection. For text input, an EditText object can be used.After it is declared, some Java code needs to capture the text entry at run-time.This is done as shown in Listing 4.14. Note that the content of the text entry textResult in this example should not be modified.A copy of the content can be made in case modification is needed.

Listing 4.14 Capturing Text from an EditText Object

CharSequence phoneNumber;

EditText textResult = (EditText) findViewById(; textResult.setOnKeyListener(new OnKeyListener() {

public boolean onKey(View v, int keyCode, KeyEvent event) { // register the text when "enter" is pressed if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // grab the text for use in the activity phoneNumber = textResult.getText(); return true;

return false;

Returning true from the onKey method indicates to the super function that the key press event was consumed (utilized), and there is no need to process further.

To provide user selection of different options normally used in forms, the use of standard widgets such as checkboxes, radio buttons, and drop-down selection menus are implemented using widgets as shown in the next section.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment