📄 newrecordform.java
字号:
package PhoneBook;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class NewRecordForm extends Form implements CommandListener {
private TextField nameField;
private TextField mobileField;
private TextField phoneField;
private TextField emailField;
private Displayable dis;
private MIDlet mid;
private RecordStore rs =null;
private static final Command saveCommand=new Command ("保存",Command.OK,2);
private static final Command backCommand=new Command("返回",Command.BACK,3);
public NewRecordForm (String title,Displayable dis,MIDlet mid ,RecordStore rs){
super(title);
this.dis=dis;
this.mid=mid;
this.rs=rs;
nameField= new TextField(BookConstant.name,null,25,TextField.ANY);
mobileField =new TextField (BookConstant.mobile,null,25,TextField.PHONENUMBER);
phoneField=new TextField(BookConstant.phone,null,25,TextField.PHONENUMBER);
emailField=new TextField(BookConstant.email,null,25,TextField.EMAILADDR);
this.append(nameField);
this.append(mobileField);
this.append(phoneField);
this.append(emailField);
this.addCommand(saveCommand);
this.addCommand(backCommand);
this.setCommandListener(this);
}
public void commandAction(Command c,Displayable d)
{
if(c==saveCommand){
if(rs!=null){
if(nameField.getString().length()==0){
Alert a_name=new Alert("提醒","姓名不能为空,请输入姓名",null,AlertType.ERROR);
a_name.setTimeout(Alert.FOREVER);
Display.getDisplay(mid).setCurrent(a_name);
return;
}
RecordFilter rf=new FindFilter(nameField.getString());
RecordEnumeration re=null;
try{
re=rs.enumerateRecords(rf,null,false);
}catch (Exception e){
}
if(re.numRecords()>0){
Alert a_same=new Alert("提醒","你输入的姓名已经存在,请输入其他名字",null,AlertType.ERROR);
a_same.setTimeout(Alert.FOREVER);
Display.getDisplay(mid).setCurrent(a_same);
re.destroy();
return;
}
BookAccount account=new BookAccount(nameField.getString(),mobileField.getString(),emailField.getString(),phoneField.getString());
try{
byte[] rec=account.serialize();
rs.addRecord(rec,0,rec.length);
Display.getDisplay(mid).setCurrent(dis);
}catch (Exception e){
}
}
}else if(c==backCommand){
Display.getDisplay(mid).setCurrent(dis);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -