|
package com;
![](/Images/OutliningIndicators/None.gif)
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;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /** *//*******************************************************************************
*
* @author zdw
*
*/
public class TextTest extends MIDlet implements CommandListener
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
// 文本框
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;
![](/Images/OutliningIndicators/InBlock.gif)
public TextTest()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
// 初始化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);
![](/Images/OutliningIndicators/InBlock.gif)
tbx.addCommand(clear);
tbx.addCommand(getCursorPos);
tbx.addCommand(astrict);
tbx.addCommand(send);
tbx.addCommand(exit);
// 添加Ticker(显示在TextBox上方)
tbx.setTicker(new Ticker("短信编辑器"));
// 添加事件监听器
tbx.setCommandListener(this);
}
![](/Images/OutliningIndicators/InBlock.gif)
// Alert的初始函数
public void initAlert()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Alert alert = new Alert("提示", "发送成功", null, AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
![](/Images/OutliningIndicators/InBlock.gif)
// 事件处理
public void commandAction(Command cmd, Displayable dis)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (cmd == getCursorPos)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
System.out.println("光标位置为:" + tbx.getCaretPosition());
}
if (cmd == clear)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
tbx.setString("");
}
if (cmd.getLabel().equals("只能输入数字"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
tbx.setConstraints(TextField.DECIMAL);
tbx.removeCommand(astrict);
astrict = new Command("取消限制", Command.SCREEN, 1);
tbx.addCommand(astrict);
}
if (cmd.getLabel().equals("取消限制"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
tbx.setConstraints(TextField.ANY);
tbx.removeCommand(astrict);
astrict = new Command("只能输入数字", Command.SCREEN, 1);
tbx.addCommand(astrict);
}
if (cmd.getCommandType() == Command.EXIT)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.notifyDestroyed();
}
if (cmd == send)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.initAlert();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void pauseApp()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void startApp() throws MIDletStateChangeException
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
|