- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- /**
- * @author Ting
- */
- public class Exec extends MIDlet implements CommandListener {
- Display display;
- public static List list;
- List l1, l2, l3, l4;
- TextField name, phoneNo, address, others;
- TextField name1, phoneNo1, address1, others1;
- private Form addForm, editForm;
- public static String[] na, pn, adds, otrs;
- Command cmdAppend = new Command("追加于后", Command.OK, 1);
- Command cmdInsert = new Command("插入于前", Command.OK, 1);
- Command cmdSelect = new Command("查看", Command.BACK, 1);
- Command cmdOK1 = new Command("确认", Command.OK, 1);
- Command cmdOK2 = new Command("确认", Command.OK, 1);
- Command cmdDelete = new Command("删除", Command.BACK, 1);
- Command flag;
- public void startApp() {
- na = new String[100];
- pn = new String[100];
- adds = new String[100];
- otrs = new String[100];
- display = Display.getDisplay(this);
- list = new List("通讯录", Choice.IMPLICIT);
- l1 = new List("", Choice.IMPLICIT);
- l2 = new List("", Choice.IMPLICIT);
- l3 = new List("", Choice.IMPLICIT);
- l4 = new List("", Choice.IMPLICIT);
- list.addCommand(cmdAppend);
- list.addCommand(cmdInsert);
- list.addCommand(cmdSelect);
- list.setCommandListener(this);
- addForm = new Form("添加联系人");
- name = new TextField("姓名: ", "", 50, TextField.ANY);
- phoneNo = new TextField("电话号码:", "", 20, TextField.PHONENUMBER);
- address = new TextField("通讯地址:", "", 80, TextField.ANY);
- others = new TextField("备注:", "", 50, TextField.ANY);
- addForm.append(name);
- addForm.append(phoneNo);
- addForm.append(address);
- addForm.append(others);
- addForm.addCommand(cmdOK1);
- addForm.setCommandListener(this);
- editForm = new Form("联系人");
- name1 = new TextField("姓名: ", "", 50, TextField.UNEDITABLE);
- phoneNo1 = new TextField("电话号码:", "", 20, TextField.UNEDITABLE);
- address1 = new TextField("通讯地址:", "", 80, TextField.UNEDITABLE);
- others1 = new TextField("备注:", "", 50, TextField.UNEDITABLE);
- editForm.append(name1);
- editForm.append(phoneNo1);
- editForm.append(address1);
- editForm.append(others1);
- editForm.addCommand(cmdOK2);
- editForm.addCommand(cmdDelete);
- editForm.setCommandListener(this);
- display.setCurrent(list);
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- }
- public void commandAction(Command c, Displayable s) {
- if (c == cmdAppend || c == cmdInsert) {
- flag = c;
- name.setString("");
- phoneNo.setString("");
- address.setString("");
- others.setString("");
- display.setCurrent(addForm);
- } else if (c == cmdOK1) {
- int index = list.getSelectedIndex();
- String result = name.getString();
- String r1 = result;
- String r2 = phoneNo.getString();
- String r3 = address.getString();
- String r4 = others.getString();
- if (index < 0) {
- list.append(result, null);
- l1.append(r1, null);
- l2.append(r2, null);
- l3.append(r3, null);
- l4.append(r4, null);
- } else {
- if (flag == cmdAppend) {
- list.insert(index + 1, result, null);
- l1.insert(index + 1, r1, null);
- l2.insert(index + 1, r2, null);
- l3.insert(index + 1, r3, null);
- l4.insert(index + 1, r4, null);
- } else {
- list.insert(index, result, null);
- l1.insert(index, r1, null);
- l2.insert(index, r2, null);
- l3.insert(index, r3, null);
- l4.insert(index, r4, null);
- }
- }
- display.setCurrent(list);
- } else if (c == cmdOK2) {
- display.setCurrent(list);
- } else if (c == cmdSelect) {
- int index = list.getSelectedIndex();
- if (index < 0) {
- Alert alert = new Alert("通知");
- alert.setType(AlertType.INFO);
- alert.setTimeout(1000);
- alert.setString("没有记录");
- display.setCurrent(alert);
- } else {
- int in = list.getSelectedIndex();
- name1.setString(l1.getString(in));
- phoneNo1.setString(l2.getString(in));
- address1.setString(l3.getString(in));
- others1.setString(l4.getString(in));
- display.setCurrent(editForm);
- }
- } else if (c == cmdDelete) {
- int index = list.getSelectedIndex();
- list.delete(index);
- l1.delete(index);
- l2.delete(index);
- l3.delete(index);
- l4.delete(index);
- }
- display.setCurrent(list);
- }
- }
手机通讯录的简单实现
最新推荐文章于 2023-05-29 21:49:48 发布