Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

MIDP's User Interface Hierarchy: TextBox

From Wiki
Jump to: navigation, search
Article Metadata
Article
Created: thiagobrunoms (09 Oct 2009)
Last edited: hamishwillee (01 Aug 2013)

In our first part of this series, we discussed about the MIDP's User Interface Hierarchy and an overview of the main UI components. In this second part, I'll take over the TextBox component.

Introduction

Imagine yourself writing an email text in your mobile device. It is not required to say that you need SPACE and as many lines as you can (or your device can provide!). It is here where the TextBox component comes in. A TextBox is a multi-line text entry screen. The table below depicts the main TextBox-based methods:

TableTextBox.png

Code Example

Our code example allows a user to write a text and, when pressing "Ok" command, all text from the beginning to the current cursor position will be copied and pasted to the end of the current text. To create a new TextBox, you can extend the class TextBox, such as:

public class MyTextBox extends TextBox implements CommandListener {
 
//A Command object
private Command cmdOk;
 
/*
* Invokes the TextBox constructor
* title The title of the TextBox
* text The current text
* maxSize The maximum character
* constraints The text constraint
*/

public MyTextBox(String title, String text, int maxSize, int constraints) {
 
//Invokes the TextBox constructor
super(title, text, maxSize, constraints);
 
//Creates a new Command, with "Ok" as its title
cmdOk = new Command("Ok", Command.OK, 1);
//Adds the Ok command to this TextBox
addCommand(cmdOk);
//This TextBox will be its CommandListener
setCommandListener(this);
}
 
public void commandAction(Command cmd, Displayable arg1) {
int current = getCaretPosition(); //Gets the current cursor position
int size = size(); //Gets the current text size
String currentContent = this.getString(); //Gets the current text
 
//Creates a new text which is the concatenation from the beginning to the current cursor position
String newContent = " " + currentContent.substring(0, current);
 
//Checks if we append a new string it will exceed the maximum number of characters
if((newContent.length() + currentContent.length()) < getMaxSize())
insert(newContent, size); //appends the newString object
 
}
 
}

To use the MyTextBox class, it is simple and straightforward:

//Creates a new MyTextBox reference, with "Thiagobrunoms" as its current text,
// 50 as the maximum text size and TextField.ANY to allow any text character
MyTextBox textBox = new MyTextBox("Title", "Thiago Sales", 50, TextField.ANY);
 
//Sets the current displayable as the textBox reference.
display.setCurrent(textBox);

The pictures below shows the initial text and, after pressing "Ok", a new text.

TextBoxExample1.jpg TextBoxExample2.jpg

This second part of this series introduced the TextBox UI component. The next part I will talk over about the List Screen-based component (MIDP's User Interface Hierarchy: List).

This page was last modified on 1 August 2013, at 06:54.
54 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×