Package chapter04;
Import javax. microedition. lcdui. choice;
Import javax. microedition. lcdui. choicegroup;
Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. commandlistener;
Import javax. microedition. lcdui. display;
Import javax. microedition. lcdui. displayable;
Import javax. microedition. lcdui. form;
Import javax. microedition. lcdui. item;
Import javax. microedition. lcdui. itemstatelistener;
Import javax. microedition. lcdui. textbox;
Import javax. microedition. MIDlet. MIDlet;
Import javax. microedition. MIDlet. midletstatechangeexception;
/*
* Author: jcuckoo
* Date: 2008-10-27
* Function: Multi-choice mode, multi-interface implementation, screen switching exercise
*/
Public class mulchoicegroupmidlet extends MIDlet implementsItemstatelistener, commandlistener{
Private display;
Private form;
Private textbox;
Private command exitcommand;
// Radio Mode
// Private choicegroup CG = new choicegroup ("Salary:", choice. Exclusive );
// Zero or multiple choice mode
Private choicegroup CG = new choicegroup ("wage level:", choice. Multiple );
// Pop-up mode
// Private choicegroup CG = new choicegroup ("wage level:", choice. Popup );
Public mulchoicegroupmidlet (){
Display = display. getdisplay (this );
Form = new form ("");
Textbox = new Textbox ("the selected content is:", "", 0 );
Exitcommand = new command ("return", command. screen, 1 );
// Add options to the CG component
CG. append ("less than 600", null );
CG. append ("600 ~ 1000 ", null );
CG. append ("3000 ~ 5000 ", null );
CG. insert (2, "1000 ~ 3000 ", null );
// Delete the specified option
// CG. Delete (1 );
// Delete all options
// CG. deleteall ();
// Add CG to form
Form. append (CG );
// Register a listener event
Form. setitemstatelistener (this );
// Add the exit button to textbox
Textbox. addcommand (exitcommand );
// Register a listener event
Textbox. setcommandlistener (this );
}
Protected void destroyapp (Boolean arg0) throws midletstatechangeexception {
}
Protected void pauseapp (){
}
Protected void Startapp () throws midletstatechangeexception {
Display. setcurrent (form );
}
Public void itemstatechanged (item ){
String STR = "";
// Obtain the CG size of the component.
Int num = CG. Size ();
System. Out. println (Num );
// Create a Boolean array to store the statuses of the CG component.
Boolean [] status = new Boolean [num];
// Obtain the status of CG and put it in the status array.
CG. getselectedflags (Status );
// Cyclically display the content in the array
For (INT I = 0; I <num; I ++ ){
System. Out. println (status [I]);
}
// Obtain the content that is true (that is, the selected option) in the array.
For (INT I = 0; I <num; I ++ ){
If (status [I]) {
STR = STR + CG. getstring (I) + "/N ";
}
}
// Use textbox to display the corresponding content
Textbox. setstring (STR );
// Use the screen manager to switch the screen
Display. setcurrent (textbox );
}
Public void commandaction (command C, displayable d ){
If (C = exitcommand ){
// Use the screen manager to switch the screen
Display. setcurrent (form );
}
}
}