📄 datefieldtest.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Date;
public class DateFieldTest extends MIDlet implements CommandListener{
private Form form;
private List startmenu;
private long date, time;
// one day in millis
private long dayInMillis=24*60*60*1000;
private String choices[]={"DATE mode","TIME mode", "DATE_TIME mode"};
private Command backCommand = new Command("Back", Command.BACK, 1);
private Command okCommand = new Command("OK", Command.OK, 1);
private Command exitCommand = new Command("EXIT", Command.EXIT, 1);
private Display display;
public DateFieldTest() {
//create a list as start menu
startmenu= new List("Chooise a input mode",List.IMPLICIT,choices,null);
startmenu.addCommand(exitCommand);
startmenu.setCommandListener(this);
//create a form to manage a gauge
form= new Form("DateField Test");
DateField df=new DateField("",DateField.DATE_TIME);
time=System.currentTimeMillis()%dayInMillis;
date=System.currentTimeMillis()-time;
df.setDate(new Date(date+time));
form.append(df);
form.addCommand(okCommand);
form.addCommand(backCommand);
form.setCommandListener(this);
//retrieve display object
display=Display.getDisplay(this);
}
public void startApp() throws MIDletStateChangeException {
display.setCurrent(startmenu);
}
/**
* Pause the MIDlet
*/
public void pauseApp() {
}
/**
* Called by the framework before the application is unloaded
*/
public void destroyApp(boolean unconditional) {
form=null;
startmenu=null;
backCommand = null;
okCommand=null;
exitCommand = null;
display=null;
}
public void commandAction(Command c, Displayable d) {
if(d==startmenu && c==List.SELECT_COMMAND) {
DateField df= (DateField) form.get(0);
//save the date/time information has been set
if(df.getInputMode()==DateField.DATE) {
date=df.getDate().getTime();
}
else if(df.getInputMode() == DateField.TIME) {
time=df.getDate().getTime();
}
else {
time=df.getDate().getTime()%dayInMillis;
date=df.getDate().getTime()-time;
}
//set time and input mode
df.setLabel(choices[startmenu.getSelectedIndex()]);
switch(startmenu.getSelectedIndex()) {
case 0:
df.setInputMode(DateField.DATE);
df.setDate(new Date(date));
break;
case 1:
df.setInputMode(DateField.TIME);
df.setDate(new Date(time));
break;
case 2:
df.setInputMode(DateField.DATE_TIME);
df.setDate(new Date(date+time));
break;
}
display.setCurrent(form);
}
else if(d==form && c==okCommand) {//get time
DateField df= (DateField) form.get(0);
System.out.println("Time set in millis: "+df.getDate().getTime());
}
else if(c==backCommand) {
display.setCurrent(startmenu);
}
else if(c==exitCommand) {
destroyApp(true);
notifyDestroyed();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -