界面说明: 欢迎界面--包含 退出按钮 电话本按钮 欢迎说明
主界面--显示通讯录列表 包含 返回(回到欢迎界面) 按钮 删除按钮(对列表中的记录删除) 添加按钮(跳转到添加界面)
添加界面--有姓名、号码文本框 保存和返回按钮
代码说明:
1.欢迎界面 WelcomeCanvas.class
2.通讯列表 PhoneList.class
主界面--显示通讯录列表 包含 返回(回到欢迎界面) 按钮 删除按钮(对列表中的记录删除) 添加按钮(跳转到添加界面)
添加界面--有姓名、号码文本框 保存和返回按钮
代码说明:
1.欢迎界面 WelcomeCanvas.class
Code
1package org;
2
3import javax.microedition.lcdui.Canvas;
4import javax.microedition.lcdui.Command;
5import javax.microedition.lcdui.CommandListener;
6import javax.microedition.lcdui.Displayable;
7import javax.microedition.lcdui.Font;
8import javax.microedition.lcdui.Graphics;
9
10public class WelcomeCanvas extends Canvas implements CommandListener {
11
12 private Command cmdExit = new Command("退出", Command.EXIT, 1);
13 private Command cmdPhone = new Command("电话本", Command.SCREEN, 1);
14 private MainMIDlet mid;
15
16 protected void paint(Graphics g) {
17
18 g.setColor(255, 0, 0);
19 g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
20 Font.SIZE_LARGE));
21 String welcomeString = "欢迎使用";
22 int fontStringWidth = g.getFont().stringWidth(welcomeString);// 字长
23 int fontStringHeight = g.getFont().getHeight();// 高度
24 // 显示屏幕正中央
25 g.drawString(welcomeString, this.getWidth() / 2 - fontStringWidth / 2,
26 this.getHeight() / 2 - fontStringHeight / 2, Graphics.LEFT
27 | Graphics.TOP);
28 }
29
30 public WelcomeCanvas(MainMIDlet midDlet) {
31 this.addCommand(cmdExit);
32 this.addCommand(cmdPhone);
33 this.setCommandListener(this);
34 this.mid = midDlet;
35 }
36
37 public void commandAction(Command c, Displayable d) {
38
39 if (c == cmdExit) {
40 mid.notifyDestroyed();
41 }
42 if (c == cmdPhone) {
43 mid.changeCurrentUI("phoneList");
44 }
45 }
46
47}
48
1package org;
2
3import javax.microedition.lcdui.Canvas;
4import javax.microedition.lcdui.Command;
5import javax.microedition.lcdui.CommandListener;
6import javax.microedition.lcdui.Displayable;
7import javax.microedition.lcdui.Font;
8import javax.microedition.lcdui.Graphics;
9
10public class WelcomeCanvas extends Canvas implements CommandListener {
11
12 private Command cmdExit = new Command("退出", Command.EXIT, 1);
13 private Command cmdPhone = new Command("电话本", Command.SCREEN, 1);
14 private MainMIDlet mid;
15
16 protected void paint(Graphics g) {
17
18 g.setColor(255, 0, 0);
19 g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
20 Font.SIZE_LARGE));
21 String welcomeString = "欢迎使用";
22 int fontStringWidth = g.getFont().stringWidth(welcomeString);// 字长
23 int fontStringHeight = g.getFont().getHeight();// 高度
24 // 显示屏幕正中央
25 g.drawString(welcomeString, this.getWidth() / 2 - fontStringWidth / 2,
26 this.getHeight() / 2 - fontStringHeight / 2, Graphics.LEFT
27 | Graphics.TOP);
28 }
29
30 public WelcomeCanvas(MainMIDlet midDlet) {
31 this.addCommand(cmdExit);
32 this.addCommand(cmdPhone);
33 this.setCommandListener(this);
34 this.mid = midDlet;
35 }
36
37 public void commandAction(Command c, Displayable d) {
38
39 if (c == cmdExit) {
40 mid.notifyDestroyed();
41 }
42 if (c == cmdPhone) {
43 mid.changeCurrentUI("phoneList");
44 }
45 }
46
47}
48
2.通讯列表 PhoneList.class
Code
1package org;
2
3import javax.microedition.lcdui.Command;
4import javax.microedition.lcdui.CommandListener;
5import javax.microedition.lcdui.Displayable;
6import javax.microedition.lcdui.List;
7import javax.microedition.rms.RecordStoreException;
8
9public class PhoneList extends List implements CommandListener {
10
11 private Command cmdBack = new Command("返回", Command.BACK, 1);
12 private Command cmdAdd = new Command("添加", Command.SCREEN, 1);
13 private Command cmdDelete = new Command("删除", Command.SCREEN, 2);
14 private MainMIDlet mid;
15
16 public PhoneList(MainMIDlet mid) {
17 super("电话本", List.IMPLICIT);
18 this.addCommand(cmdBack);
19 this.addCommand(cmdAdd);
20 this.addCommand(cmdDelete);
21 this.setCommandListener(this);
22 this.mid = mid;
23 }
24
25 public void commandAction(Command c, Displayable d) {
26
27 //返回
28 if (c == cmdBack) {
29 mid.changeCurrentUI("welcome");
30 }
31 //添加
32 if (c == cmdAdd) {
33 mid.changeCurrentUI("PhoneAddForm");
34 }
35 //删除
36 if (c == cmdDelete) {
37 try {
38 mid.rStoreOperate.deleteRecord(this.getString(this
39 .getSelectedIndex()));
40
41 mid.LoadData();
42 } catch (RecordStoreException e) {
43 // TODO Auto-generated catch block
44 e.printStackTrace();
45 }
46 }
47
48 }
49}
50
1package org;
2
3import javax.microedition.lcdui.Command;
4import javax.microedition.lcdui.CommandListener;
5import javax.microedition.lcdui.Displayable;
6import javax.microedition.lcdui.List;
7import javax.microedition.rms.RecordStoreException;
8
9public class PhoneList extends List implements CommandListener {
10
11 private Command cmdBack = new Command("返回", Command.BACK, 1);
12 private Command cmdAdd = new Command("添加", Command.SCREEN, 1);
13 private Command cmdDelete = new Command("删除", Command.SCREEN, 2);
14 private MainMIDlet mid;
15
16 public PhoneList(MainMIDlet mid) {
17 super("电话本", List.IMPLICIT);
18 this.addCommand(cmdBack);
19 this.addCommand(cmdAdd);
20 this.addCommand(cmdDelete);
21 this.setCommandListener(this);
22 this.mid = mid;
23 }
24
25 public void commandAction(Command c, Displayable d) {
26
27 //返回
28 if (c == cmdBack) {
29 mid.changeCurrentUI("welcome");
30 }
31 //添加
32 if (c == cmdAdd) {
33 mid.changeCurrentUI("PhoneAddForm");
34 }
35 //删除
36 if (c == cmdDelete) {
37 try {
38 mid.rStoreOperate.deleteRecord(this.getString(this
39 .getSelectedIndex()));
40
41 mid.LoadData();
42 } catch (RecordStoreException e) {
43 // TODO Auto-generated catch block
44 e.printStackTrace();
45 }
46 }
47
48 }
49}
50
3.添加界面 PhoneNumberAddForm.class
Code
1package org;
2
3import javax.microedition.lcdui.Command;
4import javax.microedition.lcdui.CommandListener;
5import javax.microedition.lcdui.Displayable;
6import javax.microedition.lcdui.Form;
7import javax.microedition.lcdui.StringItem;
8import javax.microedition.lcdui.TextField;
9import javax.microedition.rms.RecordStoreException;
10
11public class PhoneNumberAddForm extends Form implements CommandListener {
12
13 private Command cmdAddNumber = new Command("添加号码", Command.OK, 2);
14
15 private Command cmdBack = new Command("返回", Command.BACK, 3);
16
17 private TextField txtName = new TextField("姓名", "", 50, TextField.ANY);
18
19 private TextField txtNumber = new TextField("号码", "", 50, TextField.NUMERIC);
20
21 private StringItem alertInfo = new StringItem("", "");
22
23 MainMIDlet midDlet;
24
25 public PhoneNumberAddForm(MainMIDlet mid) {
26 super("添加电话号码");
27 this.addCommand(cmdAddNumber);
28 this.addCommand(cmdBack);
29 this.append(txtName);
30 this.append(txtNumber);
31 this.append(alertInfo);
32 this.setCommandListener(this);
33 this.midDlet = mid;
34
35 }
36
37 public void commandAction(Command c, Displayable d) {
38
39 if (c == cmdBack) {
40 midDlet.changeCurrentUI("phoneList");
41 }
42 if (c == cmdAddNumber) {
43 try {
44
45 midDlet.rStoreOperate.addRecord(txtName.getString() + "-"
46 + txtNumber.getString());
47
48 alertInfo.setText("添加成功!");
49 txtName.setString("");
50 txtNumber.setString("");
51
52 } catch (RecordStoreException e) {
53 e.printStackTrace();
54 alertInfo.setText("添加失败!");
55 }
56 }
57 }
58}
59
1package org;
2
3import javax.microedition.lcdui.Command;
4import javax.microedition.lcdui.CommandListener;
5import javax.microedition.lcdui.Displayable;
6import javax.microedition.lcdui.Form;
7import javax.microedition.lcdui.StringItem;
8import javax.microedition.lcdui.TextField;
9import javax.microedition.rms.RecordStoreException;
10
11public class PhoneNumberAddForm extends Form implements CommandListener {
12
13 private Command cmdAddNumber = new Command("添加号码", Command.OK, 2);
14
15 private Command cmdBack = new Command("返回", Command.BACK, 3);
16
17 private TextField txtName = new TextField("姓名", "", 50, TextField.ANY);
18
19 private TextField txtNumber = new TextField("号码", "", 50, TextField.NUMERIC);
20
21 private StringItem alertInfo = new StringItem("", "");
22
23 MainMIDlet midDlet;
24
25 public PhoneNumberAddForm(MainMIDlet mid) {
26 super("添加电话号码");
27 this.addCommand(cmdAddNumber);
28 this.addCommand(cmdBack);
29 this.append(txtName);
30 this.append(txtNumber);
31 this.append(alertInfo);
32 this.setCommandListener(this);
33 this.midDlet = mid;
34
35 }
36
37 public void commandAction(Command c, Displayable d) {
38
39 if (c == cmdBack) {
40 midDlet.changeCurrentUI("phoneList");
41 }
42 if (c == cmdAddNumber) {
43 try {
44
45 midDlet.rStoreOperate.addRecord(txtName.getString() + "-"
46 + txtNumber.getString());
47
48 alertInfo.setText("添加成功!");
49 txtName.setString("");
50 txtNumber.setString("");
51
52 } catch (RecordStoreException e) {
53 e.printStackTrace();
54 alertInfo.setText("添加失败!");
55 }
56 }
57 }
58}
59
4.主界面MIDlet
Code
1package org;
2
3import java.util.Enumeration;
4import java.util.Vector;
5
6import javax.microedition.lcdui.Display;
7import javax.microedition.midlet.MIDlet;
8import javax.microedition.midlet.MIDletStateChangeException;
9import javax.microedition.rms.RecordStoreException;
10
11public class MainMIDlet extends MIDlet {
12
13 private Display display;
14 WelcomeCanvas wc = new WelcomeCanvas(this);
15 PhoneList pList = new PhoneList(this);
16 PhoneNumberAddForm phoneNumberAddForm = new PhoneNumberAddForm(this);
17 public RecordStoreOperate rStoreOperate = new RecordStoreOperate("number");
18
19 public MainMIDlet() {
20 display = Display.getDisplay(this);
21 }
22
23 protected void destroyApp(boolean unconditional)
24 throws MIDletStateChangeException {
25
26 }
27
28 protected void pauseApp() {
29
30 }
31
32 // /加载通讯列表
33 public void LoadData() {
34 try {
35 pList.deleteAll();
36 Vector vector = rStoreOperate.loadData();
37 Enumeration en = vector.elements();
38 while (en.hasMoreElements()) {
39 pList.append(en.nextElement().toString(), null);
40 }
41 } catch (RecordStoreException e) {
42 e.printStackTrace();
43 }
44
45 }
46
47 protected void startApp() throws MIDletStateChangeException {
48 changeCurrentUI("welcome");
49 }
50
51 public void changeCurrentUI(String name) {
52 if (name.equals("phoneList")) {
53 display.setCurrent(pList);
54 LoadData();
55 }
56 if (name.equals("welcome")) {
57 display.setCurrent(wc);
58 }
59 if (name == "PhoneAddForm") {
60 display.setCurrent(phoneNumberAddForm);
61
62 }
63 }
64
65}
66
1package org;
2
3import java.util.Enumeration;
4import java.util.Vector;
5
6import javax.microedition.lcdui.Display;
7import javax.microedition.midlet.MIDlet;
8import javax.microedition.midlet.MIDletStateChangeException;
9import javax.microedition.rms.RecordStoreException;
10
11public class MainMIDlet extends MIDlet {
12
13 private Display display;
14 WelcomeCanvas wc = new WelcomeCanvas(this);
15 PhoneList pList = new PhoneList(this);
16 PhoneNumberAddForm phoneNumberAddForm = new PhoneNumberAddForm(this);
17 public RecordStoreOperate rStoreOperate = new RecordStoreOperate("number");
18
19 public MainMIDlet() {
20 display = Display.getDisplay(this);
21 }
22
23 protected void destroyApp(boolean unconditional)
24 throws MIDletStateChangeException {
25
26 }
27
28 protected void pauseApp() {
29
30 }
31
32 // /加载通讯列表
33 public void LoadData() {
34 try {
35 pList.deleteAll();
36 Vector vector = rStoreOperate.loadData();
37 Enumeration en = vector.elements();
38 while (en.hasMoreElements()) {
39 pList.append(en.nextElement().toString(), null);
40 }
41 } catch (RecordStoreException e) {
42 e.printStackTrace();
43 }
44
45 }
46
47 protected void startApp() throws MIDletStateChangeException {
48 changeCurrentUI("welcome");
49 }
50
51 public void changeCurrentUI(String name) {
52 if (name.equals("phoneList")) {
53 display.setCurrent(pList);
54 LoadData();
55 }
56 if (name.equals("welcome")) {
57 display.setCurrent(wc);
58 }
59 if (name == "PhoneAddForm") {
60 display.setCurrent(phoneNumberAddForm);
61
62 }
63 }
64
65}
66
5.RMS操作
Code
1package org;
2
3import java.util.Vector;
4
5import javax.microedition.rms.RecordEnumeration;
6import javax.microedition.rms.RecordFilter;
7import javax.microedition.rms.RecordStore;
8import javax.microedition.rms.RecordStoreException;
9import javax.microedition.rms.RecordStoreNotOpenException;
10
11public class RecordStoreOperate implements RecordFilter {
12
13 private RecordStore rStore = null;
14
15 public RecordStoreOperate(String rsName) {
16
17 try {
18 rStore = RecordStore.openRecordStore(rsName, true);
19 } catch (RecordStoreException e) {
20 e.printStackTrace();
21 }
22 }
23
24 public Vector loadData() throws RecordStoreNotOpenException,
25 RecordStoreException {
26
27 Vector v = new Vector();
28
29 RecordEnumeration re = rStore.enumerateRecords(this, null, false);
30 while (re.hasNextElement()) {
31 String str = new String(re.nextRecord());
32 v.addElement(str);
33 }
34 return v;
35 }
36
37 // /删除记录 by ID
38 public void deleteRecord(int id) throws RecordStoreException {
39 rStore.deleteRecord(id);
40 }
41
42 /** *//**
43 * 删除记录 注意点 删除记录后 该记录的recordId 还是保存着的 所以删除不能把记录的numRecord当做RecordId来用
44 * 我这里采取置为空字符窜" " 而且长度要大于0 如果为"" 读取的时候就会当做NULL 而且此处也并非真正意思上的删除
45 * 删除记录也不是真正意义上的删除 只是把值改成" " 读取过滤
46 * **/
47
48 public void deleteRecord(String record) throws RecordStoreException {
49
50 int numRecords = rStore.getNumRecords();
51
52 for (int i = 0; i < numRecords; i++) {
53 byte[] b = rStore.getRecord(i + 1);
54
55 if (new String(b).equals(record)) {
56
57 rStore.setRecord(i + 1, " ".getBytes(), 0, 1);
58 }
59 }
60
61 }
62
63 // 添加记录
64 public void addRecord(String rsString) throws RecordStoreException {
65 byte[] b = rsString.getBytes();
66 rStore.addRecord(b, 0, b.length);
67 }
68
69
70 /** *//**过滤 " " 意为" "是已被删除
71 **返回true 记录集中包含该条件的记录 flase返回不符此条件的记录
72 **/
73 public boolean matches(byte[] value) {
74
75 return !new String(value).equals(" ");
76 }
77}
78
1package org;
2
3import java.util.Vector;
4
5import javax.microedition.rms.RecordEnumeration;
6import javax.microedition.rms.RecordFilter;
7import javax.microedition.rms.RecordStore;
8import javax.microedition.rms.RecordStoreException;
9import javax.microedition.rms.RecordStoreNotOpenException;
10
11public class RecordStoreOperate implements RecordFilter {
12
13 private RecordStore rStore = null;
14
15 public RecordStoreOperate(String rsName) {
16
17 try {
18 rStore = RecordStore.openRecordStore(rsName, true);
19 } catch (RecordStoreException e) {
20 e.printStackTrace();
21 }
22 }
23
24 public Vector loadData() throws RecordStoreNotOpenException,
25 RecordStoreException {
26
27 Vector v = new Vector();
28
29 RecordEnumeration re = rStore.enumerateRecords(this, null, false);
30 while (re.hasNextElement()) {
31 String str = new String(re.nextRecord());
32 v.addElement(str);
33 }
34 return v;
35 }
36
37 // /删除记录 by ID
38 public void deleteRecord(int id) throws RecordStoreException {
39 rStore.deleteRecord(id);
40 }
41
42 /** *//**
43 * 删除记录 注意点 删除记录后 该记录的recordId 还是保存着的 所以删除不能把记录的numRecord当做RecordId来用
44 * 我这里采取置为空字符窜" " 而且长度要大于0 如果为"" 读取的时候就会当做NULL 而且此处也并非真正意思上的删除
45 * 删除记录也不是真正意义上的删除 只是把值改成" " 读取过滤
46 * **/
47
48 public void deleteRecord(String record) throws RecordStoreException {
49
50 int numRecords = rStore.getNumRecords();
51
52 for (int i = 0; i < numRecords; i++) {
53 byte[] b = rStore.getRecord(i + 1);
54
55 if (new String(b).equals(record)) {
56
57 rStore.setRecord(i + 1, " ".getBytes(), 0, 1);
58 }
59 }
60
61 }
62
63 // 添加记录
64 public void addRecord(String rsString) throws RecordStoreException {
65 byte[] b = rsString.getBytes();
66 rStore.addRecord(b, 0, b.length);
67 }
68
69
70 /** *//**过滤 " " 意为" "是已被删除
71 **返回true 记录集中包含该条件的记录 flase返回不符此条件的记录
72 **/
73 public boolean matches(byte[] value) {
74
75 return !new String(value).equals(" ");
76 }
77}
78