TextBox, the Simplest Screen

Team-Fly

The simplest type of screen is the TextBox, which you've already seen in action. TextBox allows the user to enter a string. Keep in mind that on a garden-variety MID, text input is a tedious process. Many MIDs only have a numeric keypad, so entering a single character is a matter of one, two, or three button presses. A good MIDlet requires minimal user input.

That said, your MIDlet may need some kind of input-perhaps a Zip code, or a short name, or some kind of password. In these cases, you'll probably want to use a TextBox.

A TextBox is created by specifying four parameters:

 public TextBox(String title, String text, int maxSize, int constraints) 

The title is used as the screen title, while text and maxSize determine the initial text and maximum size of the text box. Finally, constraints can be to restrict the user's input. Constants from the TextField class are used to specify the type of input required:

  • ANY allows any type of input.

  • NUMERIC restricts the input to numbers.

  • PHONENUMBER requires a telephone number.

  • EMAILADDR input must be an e-mail address.

  • URL input must be a Web address.

  • PASSWORD characters are not shown when entered; generally, they are represented by asterisks.

If you don't want the TextBox to perform any validation, use ANY or its numerical equivalent, 0, for the constraints parameter in the constructor.

The PASSWORD constraint may be combined with any of the other constraints using the OR operator. For example, to create a TextBox that constrained input to an e-mail address but wanted to keep the entered data hidden, you would do something like this:

 Displayable d = new TextBox("Email", "", 64,         TextField.EMAILADDR | TextField.PASSWORD); 

If you think about it, though, PASSWORD is probably more trouble than it's worth. The point of PASSWORD fields, at least on desktop machines, is to keep someone walking past your computer screen from seeing your secret password. For every character you enter, the password field shows an asterisk or some other symbol. As you type your secret password, all that shows up on the screen is a line of asterisks (so casual observers cannot see your password). On mobile phones and other small devices, this is less of a concern, as the screens are smaller and much more difficult to read than a typical desktop monitor.

Furthermore, the difficulty of entering data on a small device means that it will be hard to correctly enter passwords typing blind. Mobile phones, for example, typically require pressing keys several times to enter a single letter. On Sun's J2MEWTK emulator, pressing the '7' key twice enters the letter 'Q.' On a real device, you would have to enter a password "gandalf" with the following sequence of key presses: 4, 2, 6, 6, 3, 2, 5, 5, 5, 3, 3, 3. Without visual feedback, it would be extremely easy to make a mistake when entering a password. ("Did I press the 5 key two times or three times?") The J2MEWTK emulator shows the current character but previously typed characters are shown as asterisks. Good passwords typically have mixed case, numbers, and possibly punctuation; these would be hard to get right.


Team-Fly


Wireless Java. Developing with J2ME
ColdFusion MX Professional Projects
ISBN: 1590590775
EAN: 2147483647
Year: 2000
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net