📄 电话簿开发实例.txt
字号:
/* * AddrBookMIDlet.java * * Created on 2004年4月9日, 下午12:17 */package ground;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.rms.*;import java.io.*;import java.util.Vector;/** * An example MIDlet with simple "Hello" text and an Exit command. * Refer to the startApp, pauseApp, and destroyApp * methods so see how each handles the requested transition. * * @author Xp * @version */public class AddrBookMIDlet extends MIDlet implements CommandListener { private Display display; // The display for this MIDlet String currentForm = ""; String dbname = "addrbook"; public AddrBookMIDlet() { display = Display.getDisplay(this); } /** * Start up the Hello MIDlet by creating the TextBox and associating * the exit command and listener. */ public void startApp() { mainForm(); } /** * Pause is a no-op since there are no background activities or * record stores that need to be closed. */ public void pauseApp() { } /** * Destroy must cleanup everything not handled by the garbage collector. * In this case there is nothing to cleanup. */ public void destroyApp(boolean unconditional) { } public void mainForm() { currentForm = "mainForm"; List l = new List("我的电话本", Choice.IMPLICIT); l.append("查看所有电话", null); l.append("新增电话记录", null); l.append("查找电话记录" ,null); l.append("退出电话本", null); l.setCommandListener(this); display.setCurrent(l); } public void listAllForm(String search) //查看所有电话 { currentForm = "listAllForm"; //From listForm = new Form("查看所有记录"); AddrBook addrBook = new AddrBook(); List l = new List("查看所有记录", Choice.IMPLICIT); Command back = new Command("返回", Command.BACK, 0); Command ok = new Command("查看", Command.OK, 0); AddrBookVct addrBookVct=new AddrBookVct(); if (search == null) addrBookVct = listAddrBook(null, 1); else addrBookVct = listAddrBook(search, 3); l.addCommand(back); l.addCommand(ok); l.setCommandListener(this); //addrBook = addrBookVct.get(0); //addrBook.name.equals("No Name") if (addrBookVct.size()==0) { Alert a = new Alert("提示", "没有记录",null, null); a.setTimeout(2000); display.setCurrent(a); return; } for (int i = 0; i<addrBookVct.size(); i++) { l.append(addrBookVct.get(i).name, null); } display.setCurrent(l); } public void showTelForm(String name) //查看电话记录详细信息 { currentForm = "showTelForm"; AddrBookVct addrBookVct = listAddrBook(name, 2); Command back = new Command("返回", Command.BACK, 0); Form f = new Form("详细资料"); f.append("姓名:\n"); f.append(addrBookVct.get(0).name + "\n"); f.append("电话号码:\n"); f.append(addrBookVct.get(0).tel + "\n"); f.addCommand(back); f.setCommandListener(this); display.setCurrent(f); } public void addTelForm() //新增电话记录 { currentForm = "addTelForm"; Form addForm = new Form("新增电话记录"); TextField tfName = new TextField("姓名", "", 20, TextField.ANY); TextField tfTel = new TextField("电话", "", 20, TextField.NUMERIC); Command back = new Command("返回", Command.BACK, 0); Command ok = new Command("确定", Command.OK, 0); //tfTel.getString() addForm.append(tfName); addForm.append(tfTel); addForm.addCommand(ok); addForm.addCommand(back); addForm.setCommandListener(this); display.setCurrent(addForm); } public void searchForm() { currentForm = "searchForm"; TextBox t = new TextBox("请输入要查找的姓名", "", 20, 0); Command back = new Command("查找", Command.OK, 0); Command search = new Command("返回", Command.OK, 0); t.addCommand(back); t.addCommand(search); t.setCommandListener(this); display.setCurrent(t); } /* * Respond to commands, including exit * On the exit command, cleanup and notify that the MIDlet has been destroyed. */ public void commandAction(Command c, Displayable s) { //MainForm if (c == List.SELECT_COMMAND && currentForm.equals("mainForm")) { List temp = (List) s; switch (temp.getSelectedIndex()) { case 0: listAllForm(null); break; case 1: addTelForm(); break; case 2: searchForm(); break; case 3: destroyApp(false); //notifyDestroyed(); } } //addTelForm if (currentForm.equals("addTelForm")) { if (c.getLabel().equals("确定")) { Alert a = new Alert("提示","添加电话记录成功" , null, null); a.setTimeout(2000); Form Temp = (Form) s; TextField tf1 = (TextField)Temp.get(0); TextField tf2 = (TextField)Temp.get(1); //System.out.println(tf1.getString() + " " + tf2.getString()); //添加记录 if (tf1.getString().equals("")) { a.setString("姓名不能为空"); display.setCurrent(a); return; } if (tf2.getString().equals("")) { a.setString("电话不能为空"); display.setCurrent(a); return; } addAddrBook(tf1.getString(), tf2.getString()); //添加记录完成 display.setCurrent(a); tf1.setString(""); tf2.setString(""); } if (c.getLabel().equals("返回")) { mainForm(); } } //listAllForm if (currentForm.equals("listAllForm")) { if (c.getLabel().equals("查看")) { //System.out.println("ListAllForm show"); List temp = (List) s; //System.out.println(temp.getString(temp.getSelectedIndex())); showTelForm(temp.getString(temp.getSelectedIndex())); } if (c.getLabel().equals("返回")) { mainForm(); } } //showTelForm if (currentForm.equals("showTelForm")) { if (c.getLabel().equals("返回")) { mainForm(); } } //searchForm if (currentForm.equals("searchForm")) { if (c.getLabel().equals("返回")) { mainForm(); } if (c.getLabel().equals("查找")) { TextBox temp = (TextBox)s; //System.out.println(temp.getString()); listAllForm(temp.getString()); } } } public RecordStore openRSAnyway(String dbname) { RecordStore rs = null; if (dbname.length()>32) return null; try { rs = RecordStore.openRecordStore(dbname, true); return rs; } catch(Exception e) { return null; } } public void addAddrBook(String name, String tel) //写记录 { FriendData data = new FriendData(); data.name = name; data.tel = tel; byte[] temp = data.encode(); RecordStore rs = openRSAnyway(dbname); if (rs == null) { System.out.println("RecordStore is null"); return; } else { try { rs.addRecord(temp, 0, temp.length); rs.closeRecordStore(); } catch(Exception e) { } } } public AddrBookVct listAddrBook(String listName, int Type) { //Type 1: 查看所有 // 2:完全彼配查找 // 3:模糊查找 RecordStore rs = openRSAnyway(dbname); AddrBookVct addrBookVct = new AddrBookVct(); AddrBook addrBook = new AddrBook(); int i = 0; if (rs == null) { System.out.println("ListAddrBook rs is null"); } else { try { RecordEnumeration re = rs.enumerateRecords(null, null, false); FriendData data = new FriendData(); if (re.numRecords() == 0) { addrBook.name = "No Name"; addrBook.tel = "No Tel"; addrBookVct.add(addrBook); return addrBookVct; } while (re.hasNextElement()) { byte temp[] = re.nextRecord(); data.decode(temp); addrBook.rsid = i++; addrBook.name = data.name; addrBook.tel = data.tel; //System.out.println("List i:" + i + " name:" + data.name); switch (Type) { case 1: //查看所有 addrBookVct.add(new AddrBook((String)data.name, (String)data.tel)); break; case 2: //完全彼配查找 if (data.name.equals(listName)) { addrBookVct.add(new AddrBook((String)data.name, (String)data.tel)); } break; case 3: //模糊查找 String s = data.name.toUpperCase(); int search_i=s.indexOf(listName.toUpperCase()); if (search_i>=0) { System.out.println("DATA Name:" + data.name); addrBookVct.add(new AddrBook((String)data.name, (String)data.tel)); } System.out.println("search_i:"+ search_i +" DATA Name:" + data.name); break; } } }catch(Exception e) { System.out.println("listAddrBook is Error"); } } /*System.out.println("TEST"); for (int j=0; j<addrBookVct.size(); j++) System.out.println("J:" + j + "Name:" + addrBookVct.get(j).name);*/ return addrBookVct; } class FriendData{ String name; String tel; public FriendData() { name = "No Name"; tel = "No Tel"; } public byte[] encode() { byte[] result = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeUTF(name); dos.writeUTF(tel); result = bos.toByteArray(); dos.close(); bos.close(); } catch(Exception e) { } return result; } public void decode(byte[] data) { try { ByteArrayInputStream bis = new ByteArrayInputStream(data); DataInputStream dis = new DataInputStream(bis); name = dis.readUTF(); tel = dis.readUTF(); dis.close(); bis.close(); }catch(Exception e) { } } }class AddrBook{ int rsid; String name; String tel; public AddrBook() { rsid = 0; name = "No Name"; tel = "No Tel"; } public AddrBook(String name, String tel) { this.rsid = 0; this.name = name; this.tel = tel; }}class AddrBookVct{ Vector vctTemp = new Vector(); AddrBook addrBook; public void add(AddrBook a) { vctTemp.addElement(a); } public int size() { return vctTemp.size(); } public AddrBook get(int i) { AddrBook a; a = (AddrBook)vctTemp.elementAt(i); //System.out.println("I:" + i + " GET NAME:" + a.name); return (AddrBook)vctTemp.elementAt(i); }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -