2011-12-22 52 views
0

我有兩個LWUIT Form s(主要和更改密碼)
一個使用actionPerformed調用另一個,它的工作原理。
然後,在第二個,我需要得到一些數據,處理並返回到第一個。
要做到這一點,我試圖再次使用actionPerformed。但Ok按鈕(手機上的右按鈕)不會調用更改密碼FormactionPerformed。只需致電主FormactionPerformed即可。爲什麼?J2ME(LWUIT) - actionPerformed不叫

在代碼的其他部分,我也是這樣做的,但是MIDletForm只有一個,它可以工作。

有什麼改變?
我失去了很多時間測試代碼,但我不能找到解決辦法

這是「主」 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(); 

    } 

} 

回答

2

有了!

InfoView.actionPerformed(...),下在ChangePassword()構造的ChangePassword.addCommandListener(...)開關Constants.CHANGE_PASSWORD_COMMAND:是蓋寫由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 
... 
} 

解決方案是隻發表評論或刪除問題聲明標記代碼片段# 1以上。

我也建議在類ChangePassword內部創建backCommand,這樣它就不會被InfoView.backCommand隱藏。

+0

@MarkComix,你能解決這個問題嗎? – Vimal 2011-12-27 21:09:27