在J2ME中,如何获取表单的所有控件的引用以记录表单的控件状态更改? [英] In J2ME, How I obtain reference of all controls of the form to record form's controls state changes?

查看:90
本文介绍了在J2ME中,如何获取表单的所有控件的引用以记录表单的控件状态更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的J2ME应用程序中,我只想在用户更改任何控件的状态时,即仅当用户使用窗体的任何控件时,显示警告对话框,然后尝试取消表单而不保存键入的数据.

In my J2ME app, I want show an alert dialog box only when the user changes the state of any controls—that is, when the user uses any of the control of the form and then try to cancel the form without saving typed data.

例如,在所有5-6个表单控件中,如果用户在1-2个文本字段中键入并尝试取消表单而不将键入的数据保存到数据库中.然后,将显示一个警告框,并显示消息是否保存更改?"使用Yes, No命令.

For example among all 5-6 controls of form if the user types in 1-2 textfields and tries to cancel the form without saving that typed data into database. An alert box should then display with the message "Save changes?" with Yes, No command.

该怎么做?

这是我的代码,无法获得预期的结果:

This is my code which does not give the desired result:

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;
public class InformAboutStatechange extends MIDlet implements CommandListener
{
  Display d;
  Form frm;
  Command Save, Cancel, CancelAlrtYes, CancelAlrtNo, CancelAlrtBack;
  TextField Name, Age;
  Alert CancelAlrt;
  boolean StateChange = false;

  public InformAboutStatechange()
  {
    d = Display.getDisplay(this);
    frm = new Form("");
    Save = new Command("Save", Command.SCREEN, 1);
    Cancel = new Command("Cancel", Command.CANCEL, 2);
    Name = new TextField("Name", "", 10, 0);
    Age = new TextField("Age", "", 10, 0);
    frm.addCommand(Save);
    frm.addCommand(Cancel);
    frm.append(Name);
    frm.append(Age);
    frm.setCommandListener(this);
    d.setCurrent(frm);
  }
  public void startApp() 
  {
  }

  public void pauseApp() 
  {
  }

  public void destroyApp(boolean unconditional) 
  {
  }

  public void commandAction(Command c, Displayable dispable) 
  {
    if (c == Cancel) {
      CancelAlrt = new Alert("Application Alert","Save Changes???",null,null);
      CancelAlrtYes = new Command("Yes",Command.SCREEN, 1);
      CancelAlrtNo = new Command("No", Command.SCREEN | Command.CANCEL, 2);
      CancelAlrtBack = new Command("Back", Command.BACK, 3);
      CancelAlrt.addCommand(CancelAlrtYes);
      CancelAlrt.addCommand(CancelAlrtNo);
      CancelAlrt.addCommand(CancelAlrtBack);
      frm.setItemStateListener(new ItemStateListener() 
      {
        public void itemStateChanged(Item item) 
        {
          item.notifyStateChanged();
          if (item == Name) {
            if (item == Age) {
              StateChange = true;
            } else {
              StateChange = false;
            }
          }
        }
      });

      if (StateChange == true) {
        d.setCurrent(CancelAlrt);
        CancelAlrt.setCommandListener(new CommandListener() 
        {
            public void commandAction(Command cmd, Displayable dispable) 
            {
              if (cmd == CancelAlrtYes) {
                d.setCurrent(frm);
              } else if (cmd == CancelAlrtNo) {
                d.setCurrent(frm);
              } else if (cmd == CancelAlrtBack) {
                d.setCurrent(frm);
              }
            }
        });
      } else {
        destroyApp(true);
        notifyDestroyed();
      }
    }   
  }
}

推荐答案

您的错误导致调用setItemStateListener的时间太晚了.

Your mistake is invoking setItemStateListener too late.

在您的代码段中,侦听器是在commandAction方法中设置的,该方法仅在用户按下命令按钮时调用.目前,开始监听"为时已晚,并且您要跟踪的所有更改都已丢失.

In your code snippet, listener is set in commandAction method, which in turn is invoked only when user presses command button. At this moment, it is just too late to start "listening" and all the changes you'd want to track are already lost.

出于您的目的,应在显示表单之前(即,在调用d.setCurrent之前)设置项目状态侦听器,如下例所示:

For your purposes, item state listener should be set prior to displaying form (ie prior to invoking d.setCurrent), as in the example below:

    // ...
    frm.setCommandListener(this);
    // ---> set item state listener here
    frm.setItemStateListener(new ItemStateListener() 
    {
      public void itemStateChanged(Item item) 
      {
        item.notifyStateChanged();
        if (item == Name) {
          if (item == Age) {
            StateChange = true;
          } else {
            StateChange = false;
          }
        }
      }
    });
    d.setCurrent(frm);
    // ...


我还将代码从InformAboutStatechange构造函数移到startApp方法中,例如在教程 MIDlet生命周期->执行状态:


I would also move code from InformAboutStatechange constructor into startApp method, as explained for example in tutorial MIDlet Life Cycle -> Execution States:

构造函数通常不执行任何初始化或不执行初始化操作...通常,您将使用startApp()分配记录存储,网络连接,UI组件等...

The constructor typically does little or no initialization... Typically, you'll use startApp() to allocate record stores, network connections, UI components, and such...

这篇关于在J2ME中,如何获取表单的所有控件的引用以记录表单的控件状态更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆