问题
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.
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.
How to do this?
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();
}
}
}
}
回答1:
Your mistake is invoking setItemStateListener
too late.
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.
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);
// ...
I would also move code from InformAboutStatechange
constructor into startApp
method, as explained for example in tutorial MIDlet Life Cycle -> Execution States:
The constructor typically does little or no initialization... Typically, you'll use startApp() to allocate record stores, network connections, UI components, and such...
来源:https://stackoverflow.com/questions/13981173/in-j2me-how-i-obtain-reference-of-all-controls-of-the-form-to-record-forms-con