📄 maintext.java
字号:
package src;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainText extends Form implements CommandListener {
Display display;
List list;
TextField text1, text2, text3, text4;
RecordStore recordStore;
Account account;
Command cmdback, cmdsave, cmdread;
public MainText(Display display, List list) { // 两个界面传送参数
super("添加联系人");
this.display = display; // 两个界面传送参数
this.list = list; // 两个界面传送参数
text1 = new TextField("姓名", "", 255, TextField.ANY);
text2 = new TextField("移动电话", "", 255, TextField.PHONENUMBER);
text3 = new TextField("固定电话", "", 255, TextField.PHONENUMBER);
text4 = new TextField("电子邮件", "", 255, TextField.EMAILADDR);
cmdback = new Command("返回", Command.SCREEN, 0);
cmdsave = new Command("添加", Command.SCREEN, 0);
append(text1);
append(text2);
append(text3);
append(text4);
addCommand(cmdback);
addCommand(cmdsave);
setCommandListener(this);
recordstore(); // 必须调用这个函数
}
public void commandAction(Command c, Displayable d) {
if (c == cmdback) {
display.setCurrent(list);
}
if (c == cmdsave) {
saveAccount();
}
}
public void recordstore() {
try {
recordStore = RecordStore.openRecordStore("record", true,
RecordStore.AUTHMODE_ANY, true);
} catch (Exception e) {
}
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream outputStream=new DataOutputStream(baos);
int id=-1;
try
{
outputStream.writeUTF(text1.getString());
outputStream.writeUTF(text2.getString());
outputStream.writeUTF(text3.getString());
outputStream.writeUTF(text4.getString());
}
catch(Exception e)
{
System.out.println(e);
}
byte[]b=baos.toByteArray();
try
{
if(recordStore.getSizeAvailable()<b.length){}
id=recordStore.addRecord( b,0,b.length);
}
catch(Exception e)
{
System.out.println(e);
}
try
{
outputStream.close();
baos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
private void saveAccount() {
Account account = new Account(text1.getString(), text2.getString(),
text3.getString(), text4.getString());
// 保存联系人
try {
byte[] data = account.encode();
recordStore.addRecord(data, 0, data.length);
Alert alert = new Alert("提示", text1.getString() + "\n"
+ text2.getString() + "\n" + text3.getString() + "\n"
+ text4.getString() + "保存成功", null, AlertType.ALARM);
alert.setTimeout(10000);
display.setCurrent(alert);
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
// private void readAccount() {
// try {
// RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
// while(re.hasNextElement()) {
// byte[] data = re.nextRecord();
// Account account = Account.decode(data);
// this.append("name: " + account.getName() + "------");
// this.append("tel: " + account.getTel());
// this.append("mobile tel: " + account.getMobileTel());
// this.append("email: " + account.getEmail());
// }
// } catch (RecordStoreNotOpenException e) {
// e.printStackTrace();
// } catch (InvalidRecordIDException e) {
// e.printStackTrace();
// } catch (RecordStoreException e) {
// e.printStackTrace();
// }
//
//
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -