TextBox
Text is entered by the user using a textbox. Like the other UI elements, a
textbox has simple features that can be set based on your requirements.
A D V E R T I S E M E N T
You can
restrict the maximum number of characters that a user is allowed to enter into a
textbox, but you need to be aware of the fact that the implementation of this
value depends upon the device that you are running it on. For example, suppose
that you request that a textbox is allowed a maximum of 50 characters, by using
setMaxSize(50), but the device can only allocate a maximum of 32
characters. Then, the user of your MIDlet will only be able to enter 32
characters.
You can also constrain the text that is accepted by the textbox, as well as
modify its display using bitwise flags defined in the TextField
class. For example, to only accept email addresses in a textbox, you will need
to set the TextField.EMAILADDR flag using the method
setConstraints(). To make this field uneditable, you will need to combine
it with the TextField.UNEDITABLE flag. This is done by doing a
bitwise OR operation between these two flags:
setConstraints(TextField.EMAILADDR | TextField.UNEDITABLE);.
There are six constraint settings for restricting content: ANY,
EMAILADDR, NUMERIC, PHONENUMBER,
URL, and DECIMAL. ANY allows all kinds of text
to be entered, while the rest constrain according to their names. Similarly,
there are six constraint settings that affect the display. These are:
PASSWORD, UNEDITABLE, SENSITIVE,
NON_PREDICTIVE, INITIAL_CAPS_WORD, and
INITIAL_CAPS_SENTENCE. Not all of these settings may be functional in all
devices.
To set the contents of a textbox, you can use a couple of methods. Use
setString(String text) to set the contents with a String
value. Use insert(String text, int position) to position text where
you want it to go. Listing 3 shows how to use both these methods, along with
some constraints.
package com.j2me.part2;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
public class TextBoxExample extends MIDlet {
private TextBox txtBox1;
private TextBox txtBox2;
public TextBoxExample() {
txtBox1 = new TextBox(
"Your Name?", "", 50, TextField.ANY);
txtBox2 = new TextBox(
"Your PIN?",
"",
4,
TextField.NUMERIC | TextField.PASSWORD);
}
public void startApp() {
Display display = Display.getDisplay(this);
display.setCurrent(txtBox1);
try{
Thread.currentThread()Sleep(5000);
} catch(Exception e) {}
txtBox1.setString("Bertice Boman");
try{
Thread.currentThread()Sleep(3000);
} catch(Exception e) {}
// inserts 'w' at the 10th index to make the
// name Bertice Bowman
txtBox1.insert("w", 10);
try{
Thread.currentThread()Sleep(3000);
} catch(Exception e) {}
display.setCurrent(txtBox2);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Listing 3. Using TextBoxes
The listing creates two textboxes: one that accepts anything under 50
characters, and one that accepts only four numeric characters that are not shown
on the screen. If you try entering anything other than numbers in the
numeric-only box, the device will not accept it, but the actual behavior may
vary across actual devices.
|