J2ME学习笔记:StringItem的使用,附代码(原创)

StringItem是一个文本控件,除了可以显示文本在屏幕上以外,还可以作为文字按钮(需要绑定指定按钮)或连接控件,构造方法:item = new StringItem("Hyper-Link","http://hi.baidu.com/9prior",Item.HYPERLINK);

处理时间用ItemCommandListener()做监听器,用CommandAction(Command ,Item)来处理,以下给出一个文字连接网络程序,点击确定后将会转向连接网络的页面:

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;

public class stringItemDemo extends MIDlet implements CommandListener,ItemCommandListener {

private Display display;
private Form mainForm;
private Command httpCommand = new Command("Go",Command.ITEM,1); //item按钮类型是提示实现屏幕上某个功能
private Command okCommand = new Command("ok",Command.ITEM,1);
private Command exitCommand = new Command("Exit",Command.ITEM,1);

public stringItemDemo() {
   mainForm = new Form("String Item Demo");
                                          //创建要添加到mainForm中的StringItem对象
   StringItem item = new StringItem("标题:","StringItem具体内容");
   mainForm.append(item);
                                          //超级链接类型的Item
   item = new StringItem("Hyper-Link","http://hi.baidu.com/9prior",Item.HYPERLINK);
   item.setDefaultCommand(httpCommand); //设置了屏幕上要显示的按钮是httpCommand,等于绑定在其按钮上了,功能在此按钮实现
   item.setItemCommandListener(this); //设置了监听器
   mainForm.append(item);
                                         //按钮类型
   item = new StringItem("","确定",Item.BUTTON);
   item.setDefaultCommand(okCommand); //设置屏幕上要显示的按钮是okCommand
   item.setItemCommandListener(this);
   mainForm.append(item);
  
   mainForm.addCommand(exitCommand);
   mainForm.setCommandListener(this);
}

//处理控件事件

protected void startApp() throws MIDletStateChangeException {
   display = Display.getDisplay(this);
   display.setCurrent(mainForm);
  

}

public void commandAction(Command c, Displayable d) {
   if (c == exitCommand)
   {
    try {
     destroyApp(false);
    } catch (MIDletStateChangeException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    notifyDestroyed();
   }
  
  
}

public void commandAction(Command c, Item item) {
  
   if (c == httpCommand)
   {
    String text = "正在连接网络.....";
    //Alert a = new Alert("URL",text,null,AlertType.INFO);
    TextBox tb = new TextBox("Alert",text,15,0);
    display.setCurrent(tb); //这一步使得当前屏幕转成了tb
   }
   else
    if(c == okCommand)
    {
     String text = "你单击了【确定】...";
     TextBox tb1 = new TextBox("Alert",text,15,0);
     display.setCurrent(tb1);
    }
  
}


protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  

}

protected void pauseApp() {
  

}

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值