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.TextBox; import javax.microedition.lcdui.TextField; import javax.microedition.lcdui.Ticker; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /** */ /******************************************************************************* * * @author zdw * */ public class TextTest extends MIDlet implements CommandListener { // 文本框 private TextBox tbx = null; // 控制输入输出的类 private Display display = Display.getDisplay(this); // 命令菜单(清除) private Command clear; // 得到鼠标位置 private Command getCursorPos; // 输入限制 private Command astrict; // 发送 private Command send; // 退出 private Command exit; public TextTest() { // 初始化textBox tbx = new TextBox("测试标题", "测试内容", 200, TextField.ANY); // 设置为当前显示 display.setCurrent(tbx); // 清除菜单 clear = new Command("清空", Command.SCREEN, 1); // 光标位置菜单 getCursorPos = new Command("光标位置", Command.SCREEN, 1); // 输入限制菜单 astrict = new Command("只能输入数字", Command.SCREEN, 1); // 发送菜单 send = new Command("发送", Command.SCREEN, 1); // 退出菜单 exit = new Command("退出", Command.EXIT, 1); tbx.addCommand(clear); tbx.addCommand(getCursorPos); tbx.addCommand(astrict); tbx.addCommand(send); tbx.addCommand(exit); // 添加Ticker(显示在TextBox上方) tbx.setTicker(new Ticker("短信编辑器")); // 添加事件监听器 tbx.setCommandListener(this); } // Alert的初始函数 public void initAlert() { Alert alert = new Alert("提示", "发送成功", null, AlertType.INFO); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } // 事件处理 public void commandAction(Command cmd, Displayable dis) { if (cmd == getCursorPos) { System.out.println("光标位置为:" + tbx.getCaretPosition()); } if (cmd == clear) { tbx.setString(""); } if (cmd.getLabel().equals("只能输入数字")) { tbx.setConstraints(TextField.DECIMAL); tbx.removeCommand(astrict); astrict = new Command("取消限制", Command.SCREEN, 1); tbx.addCommand(astrict); } if (cmd.getLabel().equals("取消限制")) { tbx.setConstraints(TextField.ANY); tbx.removeCommand(astrict); astrict = new Command("只能输入数字", Command.SCREEN, 1); tbx.addCommand(astrict); } if (cmd.getCommandType() == Command.EXIT) { this.notifyDestroyed(); } if (cmd == send) { this.initAlert(); } } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { }} 々上善若水々 2008-06-23 12:55 发表评论