您的位置:首页 > 其它

如何j2ME中想用Timer和TimeTask来实现提示信息功能?

2017-01-03 01:19 387 查看
1.下面的代码是修改密码的Form

2.功能:如果没有输入密码,那么提示“密码不能为空!”,500ms后提示信息要消失。(不要用Alert来实现)

3.我的代码有问题:

当点确定按钮的速度比较快的时候,提示信息不会消失,会出现好多个“密码不能为空”
public class PasswordModifyForm extends Form implements CommandListener {

// -------信息提示

private String msg = "";

private int pos;

private static PasswordModifyForm instance;

private static TextField newPassword = null;

private static TextField newPasswordTwo = null;

private static Command ok = new Command("确定", 4, 1);

private static Command back = new Command("返回", 2, 1);

private PasswordModifyForm() {

super(" ");

// 请输入描述信息

newPassword = new TextField("输入密码", "", 12, 0);

newPasswordTwo = new TextField("再输一次", "", 12, 0);

append(newPassword);

append(newPasswordTwo);

addCommand(ok);

addCommand(back);

setCommandListener(this);

}

public static PasswordModifyForm getInstance() {

if (instance == null)

instance = new PasswordModifyForm();

return instance;

}

public void commandAction(Command c, Displayable d) {

String newp = "", newPTwo = "";

newp = newPassword.getString();

newPTwo = newPasswordTwo.getString();

if (c == ok) {

synchronized (this) {

// -----密码

if (DateTool.isEmpty(newp) || DateTool.isEmpty(newPTwo)) {

// ----密码不能为空

msg = "密码不能为空!";

StringItem si = new StringItem(msg, "");

pos = this.append(si);

// ----------

Timer timer = new Timer();

timer.schedule(new TimerTask() {

public void run() {		 

if (!DateTool.isEmpty(msg)) {

delete(pos);

msg = "";						 

}

}

}, 500);

}

 

}

} else if (c == back) {

App.showBefore();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐