/ * StringItemlet.java * * CREATED ON April 14, 2005, 4:26 pm * /
Import javax.microedition.midlet. *; import javax.microedition.lcdui. *;
/ ** * * @author Administrator * @version * / public class stringItemlet extends MIDlet implements CommandListener, ItemCommandListener {private Form aform; private Command okCommand; private Command exitCommand; private Command hllinkCommand; private Command bCommand; private Display aDisplay; private StringItem hlstringItem Private stringItem BStringItem; Private Alert hlalert; public stringitemlet () {okcommand = New Command ("OK", Command.ok, 1); EXITCOMMAND = New Command ("exit", command.exit, 1); HLLINKCOMMAND = New Command ("LINK", Command.Item, 2); bCommand = New Command ("Button", Command.Item, 2); AFORM = New Form ("stringItemtest"); // if click HyperLink "Here" , Display Analert HlstringItem = New StringItem (Null, "Here", Item.hyperLink; HlstringItem.SetItemMandListener (this); HLStringItem.SetDefault Command (HLLINKCOMMAND); BSTRINGITEM = New ST ringItem (null, "Available?", Item.BUTTON); bstringItem.setItemCommandListener (this); bstringItem.setDefaultCommand (bCommand); hlAlert = new Alert ( "Item.HYPERLINK", "You Can Call Me 800-8101234", null , AlertType.info; Balert = New Alert ("Item.Button", "The Button IS Available!", NULL, ALERTTYPE.INFO; AFORM.APpend ("any questions, please click); AFORM.Append (HLStringItem AFORM.Append (BStringItem); AFORM.ADDCommand (OKCommand); AFORM.ADDCommand (EXITCOMMAND);
aform.setCommandListener (this);} public void startApp () {aDisplay = Display.getDisplay (this); aDisplay.setCurrent (aform);} public void pauseApp () {} public void destroyApp (boolean unconditional) {} public void commandAction (Command C, Displayable D) {if (c == exitcommand) {destroyApp (false); notifydestroyed ();} else {}} public void CommandAction (Command C, Item i) {if (c == hllinkcommand) {Adisplay .SETCURRENT (HLALERT, AFORM);} else if (c == bcommand) {adisplay.setcurrent (Balert, Afe);}}} This program If there is any more new things, it is to use StringItem's appearance. Mode: HyperLink and Button. This also uses the ItemMandListener interface, which implements the CommandAction (Command C, Item I) method. The method of the method of this method is very similar to the writing and CommandAction (Command C, Displayable D), which can be seen from the program. In addition, the CommandAction (Command C, Item I) method can also be selected using the ITEM variable i, for example:
IF (i == oneItem) {
IF (c == onecommand) {
... // The program
}
Else IF (i == OtherItem) {
IF (c == OtherCommand) {
... // The program
}
In addition, when writing, forget to write AFORM.SETCOMMANDLISTENER (this); the statement, causing the EXIT button to not exit after pressing, this matter reminds me that the program is careful when programming!