📄 loginmidlet.java
字号:
package com.j2medev.chapter4;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class LoginMIDlet extends MIDlet implements CommandListener {
private Display display;
private Form loginForm;
private Form successForm;
private TextField userName;
private TextField password;
private ChoiceGroup autoLogin;
private Model model;
public static final Command connCommand = new Command("登录", Command.OK, 1);
public static final Command exitCommand = new Command("退出",Command.EXIT,2);
protected void startApp() {
initMIDlet();
Account account = model.getAccount(1);
if (account == null || !account.isAutoLogin()) {
display.setCurrent(loginForm);
} else if (account.isAutoLogin()) {
//读取RMS中的用户和密码与服务器端校验,这里省略。
display.setCurrent(successForm);
}
}
public void initMIDlet() {
model = new Model();
display = Display.getDisplay(this);
loginForm = new Form("用户登录");
userName = new TextField("用户名:", null, 20, TextField.ANY);
password = new TextField("密码:", null, 20, TextField.PASSWORD);
autoLogin = new ChoiceGroup("设置", Choice.MULTIPLE,
new String[] { "自动登录" }, null);
loginForm.append(userName);
loginForm.append(password);
loginForm.append(autoLogin);
loginForm.addCommand(connCommand);
loginForm.addCommand(exitCommand);
loginForm.setCommandListener(this);
successForm = new Form("登录成功");
successForm.addCommand(exitCommand);
successForm.setCommandListener(this);
successForm.append("您已经成功连接到服务器!");
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) {
model.closeRecordStore();
}
public void commandAction(Command cmd, Displayable displayable) {
String _userName;
String _password;
boolean auto = false;
if (cmd == connCommand) {
_userName = userName.getString();
_password = password.getString();
auto = autoLogin.isSelected(0);
if (auto) {
Account account = new Account(_userName, _password, auto);
model.saveAccount(account);
}
display.setCurrent(successForm);
}else if(cmd == exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -