我想创建小应用程序。我将使用用户名和密码登录,然后显示选项以供选择。如果选择选项,则相应显示警报。但我无法在列表中获取事件点击项。
这是代码:
public class Midlet extends MIDlet implements CommandListener{
private Display display;
private SampleCavans myCanvas;
private TextField username;
private TextField password;
private Form form;
private Command cancel;
private Command login;
private Command mNextCommand;
private List service;
String[] stringElements = { "Check Mail", "Compose","Addresses","Options","Signout","Calculator"};
public Midlet() {
display=Display.getDisplay(this);
username=new TextField("Login ID", "", 10, TextField.ANY);
password=new TextField("Password", "", 10, TextField.PASSWORD);
cancel=new Command("Cancel", Command.CANCEL, 2);
login=new Command("Login", Command.OK, 2);
form=new Form("Sign in");
form.append(username);
form.append(password);
form.addCommand(cancel);
form.addCommand(login);
form.setCommandListener(this);//add event click for form
myCanvas=new SampleCavans();
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void menu()
{
//List service=new List("Choice one",Choice.EXCLUSIVE);
service=new List("Choice one",Choice.EXCLUSIVE,stringElements,null);
service.setCommandListener(this);
//service.addCommand(mNextCommand);
display.setCurrent(service);
}
public void tryAgain()
{
Alert error=new Alert("Login Incorrect","Please try again",null,AlertType.ERROR);
error.setTimeout(Alert.FOREVER);
username.setString("");
password.setString("");
display.setCurrent(error,form);
}
public void commandAction(Command c, Displayable d) {
String lable=c.getLabel();
if(lable.equals("Cancel"))
{
destroyApp(true);
}
else if(lable.equals("Login"))
{
validateUser(username.getString(),password.getString());
}
else if(c==List.SELECT_COMMAND)
{
int index = service.getSelectedIndex();
Alert alert = new Alert("Your selection",
"You chose " + service.getString(index) + ".",
null, AlertType.INFO);
Display.getDisplay(this).setCurrent(alert, service);
}
}
private void validateUser(String name, String pass) {
if(name.equals("12")&&pass.endsWith("12"))
{
menu();
}
else
tryAgain();
}