|
package com;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
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.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/** *//**
* 简单模拟短信发送流程
*
* @author zdw
*
*/
public class MsgTest extends MIDlet implements CommandListener,
ItemStateListener
{
/**//* 欢迎界面 */
private Form formWelcome = null;
private StringItem si = null;
private Command cmdWrite = null;
private Command cmdExit = null;
/**//* 写短信界面* */
private Form formMsg = null;
private TextField tfMsg = null;
private Command cmdBack = null;
private Command cmdSend = null;
private Command cmdClear = null;
/**//* 短信发送界面 */
private Form formSend = null;
private TextField tfPhone = null;
private Command cmdOk = null;
private Command cmdSendBack = null;
/**//* 发送成功界面 */
private Alert alert = null;
private Display display = null;
public MsgTest()
{
display = Display.getDisplay(this);
}
protected void startApp() throws MIDletStateChangeException
{
/**//* 欢迎界面初始化 */
formWelcome = new Form("欢迎您使用短信系统");
display.setCurrent(formWelcome);
cmdWrite = new Command("写短信", Command.OK, 1);
cmdExit = new Command("退出", Command.EXIT, 1);
formWelcome.addCommand(cmdWrite);
formWelcome.addCommand(cmdExit);
si = new StringItem("","欢迎您短信发送系统");
formWelcome.append(si);
/**//* 写短信界面初始化 */
formMsg = new Form("写短信");
cmdBack = new Command("返回", Command.BACK, 1);
cmdSend = new Command("发送", Command.SCREEN, 1);
cmdClear = new Command("清除", Command.BACK, 1);
formMsg.addCommand(cmdBack);
formMsg.addCommand(cmdSend);
tfMsg = new TextField("编辑短信", "", 255, TextField.ANY);
// 设置TextField占据整个屏幕
tfMsg.setPreferredSize(formMsg.getWidth(), formMsg.getHeight());
formMsg.append(tfMsg);
/**//* 短信发送界面初始化 */
formSend = new Form("请输入手机号");
tfPhone = new TextField("手机号", "", 11, TextField.NUMERIC);
cmdOk = new Command("确认发送", Command.OK, 1);
cmdSendBack = new Command("返回", Command.BACK, 1);
formSend.addCommand(cmdOk);
formSend.addCommand(cmdSendBack);
formSend.append(tfPhone);
/**//* 发送成功界面初始化 */
alert = new Alert("发送成功", "恭喜您,短信发送成功", null, AlertType.INFO);
/**//* 注册事件 */
formWelcome.setCommandListener(this);
formMsg.setCommandListener(this);
formSend.setCommandListener(this);
formMsg.setItemStateListener(this);
alert.setCommandListener(this);
}
/** *//**
* Command 事件
*/
public void commandAction(Command c, Displayable dis)
{
// 退出
if (c == cmdExit)
{
this.notifyDestroyed();
}
// 写短信
if (c == cmdWrite)
{
display.setCurrent(formMsg);
}
// 后退
if (c == cmdBack)
{
display.setCurrent(formWelcome);
}
// 清除短信(逐字删除)
if (c == cmdClear)
{
int pos = tfMsg.getCaretPosition();
tfMsg.delete(pos - 1, 1);
if (tfMsg.size() == 0)
{
formMsg.removeCommand(cmdClear);
formMsg.addCommand(cmdBack);
}
}
// 发送
if (c == cmdSend)
{
display.setCurrent(formSend);
}
// 手机号输入返回
if (c == cmdSendBack)
{
display.setCurrent(formMsg);
}
// 确认发送
if (c == cmdOk)
{
display.setCurrent(alert);
}
}
/** *//**
* TextField状态事件
*/
public void itemStateChanged(Item item)
{
if (item == tfMsg)
{
if (tfMsg.size() != 0)
{
formMsg.removeCommand(cmdBack);
formMsg.addCommand(cmdClear);
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
protected void pauseApp()
{
}
}
|