首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

怎么j2ME中想用Timer和TimeTask来实现提示信息功能

2012-01-31 
如何j2ME中想用Timer和TimeTask来实现提示信息功能?1.下面的代码是修改密码的Form2.功能:如果没有输入密码

如何j2ME中想用Timer和TimeTask来实现提示信息功能?
1.下面的代码是修改密码的Form
2.功能:如果没有输入密码,那么提示“密码不能为空!”,500ms后提示信息要消失。(不要用Alert来实现)
3.我的代码有问题:
当点确定按钮的速度比较快的时候,提示信息不会消失,会出现好多个“密码不能为空”

Java code
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();        }    }}


[解决办法]
还是不一样的,你这样按下ok的时候,第一层条件过滤不了,还会新建一个StringItem,append到Form里,msg依然不为空,等于每按一次ok,显示一个时间500ms的StringItem

热点排行