/*
* stringitemlet.java
*
* created on 2005年4月14日, 下午4:26
*/
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;
private alert balert;
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.setitemcommandlistener(this);
hlstringitem.setdefaultcommand(hllinkcommand);
bstringitem=new stringitem(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 question ,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,aform);
}
}
}
这个程序如果说有什么比较新的东西的话,那就在于运用了stringitem的外观模式:hyperlink和button。由此也使用了itemcommandlistener接口,实现了commandaction(command c ,item i)方法。这个方法的方法体的写法和commandaction(command c , displayable d)很类似,这一点可以从程序中看出来。需要说明的是,commandaction(command c , item i)方法也可以使用item变量i进行选择,例如:
if (i==oneitem){
if (c==onecommand){
……// the program
}}
else if (i==otheritem){
if (c==othercommand){
…… //the program
}}
此外,该程序在编写时,忘记写aform.setcommandlistener(this);语句了,致使exit按钮按下后无法退出,这件事提醒我,编程序时要细心!