|
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;
// 發(fā)送
private Command send;
// 退出
private Command exit;

public TextTest()
 {
// 初始化textBox
tbx = new TextBox("測試標題", "測試內(nèi)容", 200, TextField.ANY);
// 設置為當前顯示
display.setCurrent(tbx);
// 清除菜單
clear = new Command("清空", Command.SCREEN, 1);
// 光標位置菜單
getCursorPos = new Command("光標位置", Command.SCREEN, 1);
// 輸入限制菜單
astrict = new Command("只能輸入數(shù)字", Command.SCREEN, 1);
// 發(fā)送菜單
send = new Command("發(fā)送", 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("短信編輯器"));
// 添加事件監(jiān)聽器
tbx.setCommandListener(this);
}

// Alert的初始函數(shù)
public void initAlert()
 {
Alert alert = new Alert("提示", "發(fā)送成功", 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("只能輸入數(shù)字"))
 {
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("只能輸入數(shù)字", 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
 {

}

}

|