【问题标题】:J2ME (LWUIT) - actionPerformed Not CalledJ2ME (LWUIT) - 未调用 actionPerformed
【发布时间】:2011-12-24 00:30:15
【问题描述】:

我有两个 LWUIT Forms(主密码和更改密码)
一个人使用actionPerformed 打电话给另一个人,它可以工作。
然后,在第二个中,我需要获取一些数据,处理并返回到第一个。
为此,我尝试再次使用actionPerformed。但是确定按钮(手机上的右按钮),不会调用更改密码FormactionPerformed。只需致电主FormactionPerformed。为什么?

在代码的其他部分,我也这样做,但使用 MIDlet 和只有一个 Form 并且它可以工作。

有什么要改变的吗?
我浪费了很多时间来测试代码,但我找不到解决方案

这是“主要”Form 的代码:

public class InfoView extends Form implements ActionListener{

    private Command backCommand;
    private Command changePasswordCommand;
    private Command okChangePasswordForm;

     private ChangePassword changePasswordForm;

    public InfoView(Command backC){

        super();

        this.addCommand(backC);
        this.setBackCommand(backC);

        this.setScrollableY(true);

        this.setTitle(LanguageManager.getText("RootTitle"));

        changePasswordCommand = new Command(LanguageManager.getText("ChangePassword"), Constants.CHANGE_PASSWORD_COMMAND);

        okChangePasswordForm = new Command(LanguageManager.getText("Ok"), Constants.OK_CHANGE_PASSWORD_COMMAND);

        this.addAllCommands();       

        this.initForm();
    }

    public void initForm(){

        Style s = this.getStyle();

        s.setMargin(0, 0, 0, 0);
        s.setPadding(0, 0, 0, 0);

        this.addCommandListener(this);

        backCommand = new Command(LanguageManager.getText("Back"), Constants.BACK_COMMAND);

    }

    private void addAllCommands(){

        this.addCommand(changePasswordCommand);

        this.addCommand(changeContactInfoCommand);
    }

    public void actionPerformed(ActionEvent arg0) {

        //Obtengo la Opción seleccionada
        Command cmd = arg0.getCommand();

        if (cmd == null) {
            return;
        }

        System.out.println(String.valueOf(cmd.getId()));

        switch (cmd.getId()) {

            case Constants.CHANGE_PASSWORD_COMMAND:

                System.out.println("CHANGE_PASSWORD_COMMAND");

                //this.setGlassPane(null);

                if (changePasswordForm == null) {
                    changePasswordForm = new ChangePassword();
                    changePasswordForm.addCommand(backCommand);
                    changePasswordForm.addCommand(okChangePasswordForm);
                    changePasswordForm.addCommandListener(this);
                    changePasswordForm.setBackCommand(backCommand);
                }

                changePasswordForm.show();
                arg0.consume();
                break;

            case Constants.BACK_COMMAND:

                System.out.println("BACK_COMMAND");

                //20111004 MB: Cuando vuelvo, desincronizo para que al 
                //cambiar de tarjeta funcione
                //protocolManager.deSync();

                addAllCommands();

                this.show();

                this.editInfoForm = null;
                this.changePasswordForm = null;

                System.gc();

                break; 

            case Constants.OK_CHANGE_PASSWORD_COMMAND:

                System.out.println("OK_CHANGE_PASSWORD_COMMAND");

                this.editInfoForm = null;

                this.show();

                System.gc();

                break;                
        }
    }
}

这是更改密码Form的代码:

import com.sun.lwuit.Form;
import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import Project.language.LanguageManager;

public class ChangePassword extends Form implements ActionListener {

    private TextField oldPassword;
    private TextField newPassword;
    private TextField repeatNewPassword;

    public ChangePassword(){

        super ();

        this.setTitle(LanguageManager.getText("ChangePasswordTitle"));

        addCommandListener(this);

        this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        this.addComponent(new Label(LanguageManager.getText("Actual")));

        oldPassword = new TextField("");
        oldPassword.setConstraint(TextArea.PASSWORD);

        this.addComponent(oldPassword);

        this.addComponent(new Label(LanguageManager.getText("New")));

        newPassword = new TextField("");
        newPassword.setConstraint(TextArea.PASSWORD);

        this.addComponent(newPassword);

        this.addComponent(new Label(LanguageManager.getText("Repeat")));

        repeatNewPassword = new TextField("");
        repeatNewPassword.setConstraint(TextArea.PASSWORD);

        this.addComponent(repeatNewPassword);
    }

    private String getOldPass(){
        return this.oldPassword.getText();
    }

    private String getNewPass(){
        return this.newPassword.getText();
    }

    public void actionPerformed(ActionEvent arg0) {

        Command cmd = arg0.getCommand();

        String oldPasswordVar = getOldPass();
        String newPasswordVar = getNewPass();

    }

}

【问题讨论】:

    标签: java-me lwuit


    【解决方案1】:

    知道了!

    InfoView.actionPerformed(...) 中,在ChangePassword() 构造函数中的开关Constants.CHANGE_PASSWORD_COMMAND: 下,ChangePassword.addCommandListener(...)changePasswordForm.addCommandListener(this); 覆盖 因此,当显示ChangePassword 表单时InfoView.actionPerformed(...) 的 keyEvents 处于活动状态。

    代码片段 #1

    ...
    case Constants.CHANGE_PASSWORD_COMMAND:
        changePasswordForm = new ChangePassword();
        changePasswordForm.addCommand(backCommand);
        changePasswordForm.addCommandListener(this); //<-- PROBLEM, because 'this' 
                                                     //          is referring to
                                                     //          'InfoView' and NOT
                                                     //          'ChangePassword'
        changePasswordForm.setBackCommand(backCommand);
    ...
    

    代码片段 #2

    public ChangePassword(){
     ...
     addCommandListener(this);   //<-- Being over-written
     ...
    }
    

    解决方案 只是注释或删除上面代码 sn-p #1 中标记的 PROBLEM 语句。

    我还建议在 ChangePassword 类中创建 backCommand,这样它就不会被 InfoView.backCommand 隐藏。

    【讨论】:

      猜你喜欢
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多