|
|
如题。我的Sample Code 如下。我希望用户选择了日期后(例如:08/08/2010)按OK键后; 另外一个Form显示08/08/2010.但当我按Back回去上一个Form重新选择日期(例如:09/09/2009),然后按OK键,另外一个Form还是显示08/08/2010。我要如何做到每一次都显示新的日期呢?谢谢。
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class Date_Example1 extends MIDlet implements CommandListener {
private boolean midletPaused = false;
private Form frmMain, respresultForm;
private DateField CurrdateField;
private Command okCommand, exitCommand, backCommand;
public Date_Example1() {}
private void initialize() {}
public void startMIDlet() {
switchDisplayable(null, getFrmMain());
}
public void resumeMIDlet() {}
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
}
public void commandAction(Command command, Displayable displayable) {
if (displayable == frmMain) {
if (command == exitCommand) {
exitMIDlet();
} else if (command == okCommand) {
Result(CurrdateField.getDate());
}
}
else if (displayable == respresultForm) {
if (command == backCommand) {
switchDisplayable(null, getFrmMain());
}
}
}
public Form getFrmMain() {
if (frmMain == null) {
CurrdateField = new DateField("Current Date", DateField.DATE);
okCommand = new Command("Ok", Command.OK, 0);
exitCommand = new Command("Exit", Command.EXIT, 0);
frmMain = new Form("Welcome");
frmMain.append(CurrdateField);
frmMain.addCommand(okCommand);
frmMain.addCommand(exitCommand);
frmMain.setCommandListener(this);
}
CurrdateField.setDate(new java.util.Date(System.currentTimeMillis()));
return frmMain;
}
public void Result(final Date mdate){
Calendar c = Calendar.getInstance();
c.setTime(mdate);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DATE);
String Reqdate = (day<10? "0": "")+day+"/"+(m<10? "0": "")+m+"/"+(y<10? "0": "")+y;
switchDisplayable(null, getRespResultForm(Reqdate));
}
public Form getRespResultForm(String Respdate){
if (respresultForm == null) {
backCommand = new Command("Back", Command.BACK, 0);
StringItem resulttxt = new StringItem("Date", Respdate);
respresultForm = new Form("Testing Date");
respresultForm.append(resulttxt);
respresultForm.addCommand(backCommand);
respresultForm.setCommandListener(this);
}
return respresultForm;
}
public Display getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
public void pauseApp() {
midletPaused = true;
}
public void destroyApp(boolean unconditional) {
}
} |
|