Java Reference
In-Depth Information
ANY
Allows any text to be added.
NUMERIC
Allows integer values.
PASSWORD
L
ets the user enter a password, where the entered text is masked.
PHONENUMBER
L
ets the user enter a phone number.
URL
Allows a valid URL.
We will now show the usage of
TextField
s by creating a simple example
Form
for bank transfers.
A bank transfer form contains at least the amount of money to be transferred and the name of the
receiver.
To start the implementation of the
TeleTransfer
MIDlet, you first need to import the two packages
containing the
midlet
and
lcdui
classes:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
Every MID application is derived from
MIDlet
, so you need to extend the
MIDlet
class, too:
public class TeleTransfer extends MIDlet {
Because you want to create a
Form
that contains
Item
s for entering the transfer information, you need
a corresponding member variable
mainForm
. You can already initialize the variable at its declaration
because it has no dependencies from constructor parameters:
Form mainForm = new Form ("TeleTransfer");
In order to let the user enter the transfer information, add
TextField
s for the name of the receiver
for entering the amount to be transferred. Because of the lack of floating-point values in the CLDC, the
numeric
TextField
s in MIDP can hold integer values only. So you need to split the amount into
separate fields for dollars and cents. An alternative would be to use an alphanumeric field and parse the
string into two separate values. However, this may result in switching the keyboard to alpha mode on
cell phones, making numeric input unnecessarily complicated. In this case, you'll limit the size of
possible values to five digits for the whole dollar part and two digits for the fractional cent part. Again,
you initialize the variables where they are declared:
TextField receiverName = new TextField
("Receiver Name", "", 20, TextField.ANY);
TextField receiverAccount = new TextField
("Receiver Account#", "", 12, TextField.NUMERIC);
TextField amountWhole = new TextField ("Dollar", "", 6,
TextField.NUMERIC);
TextField amountFraction = new TextField ("Cent", "", 2,
TextField.NUMERIC);
Finally, you add a variable storing the
Display
instance for your application:
Display display = Display.getDisplay (this);
Now you can add the constructor to your application where you added the previous
TextField
s to
the main form:
public TeleTransfer() {
mainForm.append (receiverName);
mainForm.append (receiverAccount);
Search WWH ::
Custom Search