在J2ME中获取事件单击列表项

时间:2013-01-30 20:15:40

标签: java-me midp lcdui

我想创建小应用。我将使用用户名和密码登录,然后显示选项以进行选择。如果选择选项,则显示警报。但我无法在列表中获得事件点击项。

这是代码:

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

1 个答案:

答案 0 :(得分:2)

在midp中,列表实际上更像是一个包含选择选项的菜单。您必须在其上设置命令,以便在命令操作中可以调度此命令,从菜单中获取选择并相应地设置下一个屏幕。这是与您尝试的类似的代码:

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class Login extends MIDlet implements CommandListener {

    private static final String ONE = "One";
    private static final String THE_OTHER = "The Other";


    private Form form;
    private Command cancel=new Command("Cancel", Command.CANCEL, 2);
    private Command login=new Command("Login", Command.OK, 2);
    private TextField username=new TextField("Login ID", "", 10, TextField.ANY);
    private TextField password=new TextField("Password", "", 10, TextField.PASSWORD);

    private List menu = new List("Menu", List.IMPLICIT, new String[] {"One", "The Other"}, null);
    private Command selection=new Command("SELECTION", Command.ITEM, 1);

    private TextBox message1= new TextBox("One", ONE, ONE.length(), TextField.UNEDITABLE);
    private TextBox message2= new TextBox("The Other", THE_OTHER, THE_OTHER.length(), TextField.UNEDITABLE);


    public Login() {
        form=new Form("Sign in");
        form.append(username);
        form.append(password);
        form.addCommand(cancel);
        form.addCommand(login);
        form.setCommandListener(this);       
        menu.setSelectCommand(selection);
        menu.setCommandListener(this);
        message1.addCommand(cancel);
        message1.setCommandListener(this);
        message2.addCommand(cancel);
        message2.setCommandListener(this);
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException { 
        username.delete(0, username.size());
        password.delete(0, password.size());
    }

    protected void pauseApp() { }

    protected void startApp() throws MIDletStateChangeException {
        Display.getDisplay(this).setCurrent(form);
    }

    public void commandAction(Command c, Displayable d) {
        if(c == cancel) {
            if(d == form) {
                notifyDestroyed();
            }
            else {
                Display.getDisplay(this).setCurrent(form);
            }
        }
        if(c == login) {
            char[] data1 = new char[10];
            username.getChars(data1);
            char[] data2 = new char[10];
            password.getChars(data2);
            if(loginOk(new String(data1), new String(data2))) {
                Display.getDisplay(this).setCurrent(menu);
            }
            else {
                notifyDestroyed();
            }
        }
        if(c == selection) {
            if(menu.getSelectedIndex() == 0) {
                Display.getDisplay(this).setCurrent(message1);
            }
            if(menu.getSelectedIndex() == 1) {
                Display.getDisplay(this).setCurrent(message2);
            }
        }
    }

    private boolean loginOk(String string, String string2) {
        return true;
    }

}