package Prj7_1; import java.io.IOException; 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.Image;import javax.microedition.lcdui.ImageItem;import javax.microedition.lcdui.Item;import javax.microedition.lcdui.ItemStateListener;import javax.microedition.lcdui.TextBox;import javax.microedition.lcdui.TextField;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException; public class MessageMIDlet extends MIDlet implements CommandListener, ItemStateListener{ private Form welcomeForm = new Form("欢迎使用..."); private Display dis; /**************** 欢迎界面 ********************/ private Command cmdExit = new Command("退出程序",Command.BACK,1); private Command cmdWriteMsg = new Command("写短信",Command.SCREEN,1); ImageItem welcoemItem; /**************** 短信编辑界面 ********************/ private Form frmMsg = new Form("请您输入短信"); private TextField tfMsg = new TextField("请您输入短信内容","",300,TextField.ANY); private Command cmdMsgBack = new Command("返回上一页",Command.BACK,1); private Command cmdMsgDel = new Command("清除文本",Command.BACK,1); private Command cmdSend = new Command("发送",Command.SCREEN,1); /**************** 发送界面 ********************/ private TextBox tbPhone = new TextBox("输入对方号码","",15,TextField.NUMERIC); private Command cmdPhoneBack = new Command("返回",Command.BACK,1); private Command cmdOK = new Command("确定",Command.SCREEN,1); public MessageMIDlet() { /**************** 欢迎界面初始化 ********************/ Image welcomeImg = null; try { welcomeImg = Image.createImage("/Welcome.png"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } welcoemItem = new ImageItem("Welcome!",welcomeImg, Item.LAYOUT_CENTER, "无法载入图片"); /**************** 短信编辑界面初始化 ********************/ frmMsg.append(tfMsg); frmMsg.addCommand(cmdMsgBack); frmMsg.addCommand(cmdSend); /**************** 发送界面初始化 ********************/ tbPhone.addCommand(cmdPhoneBack); tbPhone.addCommand(cmdOK); /**************** 事件绑定代码 ********************/ welcomeForm.setCommandListener(this); frmMsg.setCommandListener(this); tbPhone.setCommandListener(this); frmMsg.setItemStateListener(this);//Item状态事件 } protected void startApp() throws MIDletStateChangeException { dis = Display.getDisplay(this); dis.setCurrent(welcomeForm); /**************** 欢迎界面初始化 ********************/ welcomeForm.addCommand(cmdWriteMsg); welcomeForm.addCommand(cmdExit); welcomeForm.append(welcoemItem); } public void commandAction(Command c,Displayable d){ if(c == cmdExit){ this.notifyDestroyed(); } else if(c == cmdWriteMsg){ dis.setCurrent(frmMsg); } else if(c == cmdMsgBack){ dis.setCurrent(welcomeForm); } else if(c == cmdSend){ dis.setCurrent(tbPhone); } else if(c == cmdMsgDel){ int position = tfMsg.getCaretPosition(); tfMsg.delete(position-1, 1); } else if(c == cmdPhoneBack){ dis.setCurrent(frmMsg); } else if(c == cmdOK){ //在cmdPhoneBack命令未添加处理代码时,要放在else if(c == cmdPhoneBack){后面 System.out.println("短信发送成功"); System.out.println("短信发送内容:"+tfMsg.getString()); System.out.println("短信发送目的地: "+tbPhone.getString()); } } public void itemStateChanged(Item item) { if(item == tfMsg){ System.out.println(tfMsg.size());//测试一下输入的短信字符的长度 if(tfMsg.size() == 0){ frmMsg.removeCommand(cmdMsgDel); frmMsg.addCommand(cmdMsgBack); } else{ frmMsg.removeCommand(cmdMsgBack); frmMsg.addCommand(cmdMsgDel); } } } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } }

评论