手机通讯录的简单实现

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. import javax.microedition.midlet.*;
  6. import javax.microedition.lcdui.*;
  7. /**
  8.  * @author Ting
  9.  */
  10. public class Exec extends MIDlet implements CommandListener {
  11.     Display display;
  12.     public static List list;
  13.     List l1, l2, l3, l4;
  14.     TextField name, phoneNo, address, others;
  15.     TextField name1, phoneNo1, address1, others1;
  16.     private Form addForm,  editForm;
  17.     public static String[] na,  pn,  adds,  otrs;
  18.     Command cmdAppend = new Command("追加于后", Command.OK, 1);
  19.     Command cmdInsert = new Command("插入于前", Command.OK, 1);
  20.     Command cmdSelect = new Command("查看", Command.BACK, 1);
  21.     Command cmdOK1 = new Command("确认", Command.OK, 1);
  22.     Command cmdOK2 = new Command("确认", Command.OK, 1);
  23.     Command cmdDelete = new Command("删除", Command.BACK, 1);
  24.     Command flag;
  25.     public void startApp() {
  26.         na = new String[100];
  27.         pn = new String[100];
  28.         adds = new String[100];
  29.         otrs = new String[100];
  30.         display = Display.getDisplay(this);
  31.         list = new List("通讯录", Choice.IMPLICIT);
  32.         l1 = new List("", Choice.IMPLICIT);
  33.         l2 = new List("", Choice.IMPLICIT);
  34.         l3 = new List("", Choice.IMPLICIT);
  35.         l4 = new List("", Choice.IMPLICIT);
  36.         list.addCommand(cmdAppend);
  37.         list.addCommand(cmdInsert);
  38.         list.addCommand(cmdSelect);
  39.         list.setCommandListener(this);
  40.         addForm = new Form("添加联系人");
  41.         name = new TextField("姓名: """50, TextField.ANY);
  42.         phoneNo = new TextField("电话号码:"""20, TextField.PHONENUMBER);
  43.         address = new TextField("通讯地址:"""80, TextField.ANY);
  44.         others = new TextField("备注:"""50, TextField.ANY);
  45.         addForm.append(name);
  46.         addForm.append(phoneNo);
  47.         addForm.append(address);
  48.         addForm.append(others);
  49.         addForm.addCommand(cmdOK1);
  50.         addForm.setCommandListener(this);
  51.         editForm = new Form("联系人");
  52.         name1 = new TextField("姓名: """50, TextField.UNEDITABLE);
  53.         phoneNo1 = new TextField("电话号码:"""20, TextField.UNEDITABLE);
  54.         address1 = new TextField("通讯地址:"""80, TextField.UNEDITABLE);
  55.         others1 = new TextField("备注:"""50, TextField.UNEDITABLE);
  56.         editForm.append(name1);
  57.         editForm.append(phoneNo1);
  58.         editForm.append(address1);
  59.         editForm.append(others1);
  60.         editForm.addCommand(cmdOK2);
  61.         editForm.addCommand(cmdDelete);
  62.         editForm.setCommandListener(this);
  63.         display.setCurrent(list);
  64.     }
  65.     public void pauseApp() {
  66.     }
  67.     public void destroyApp(boolean unconditional) {
  68.     }
  69.     public void commandAction(Command c, Displayable s) {
  70.         if (c == cmdAppend || c == cmdInsert) {
  71.             flag = c;
  72.             name.setString("");
  73.             phoneNo.setString("");
  74.             address.setString("");
  75.             others.setString("");
  76.             display.setCurrent(addForm);
  77.         } else if (c == cmdOK1) {
  78.             int index = list.getSelectedIndex();
  79.             String result = name.getString();
  80.             String r1 = result;
  81.             String r2 = phoneNo.getString();
  82.             String r3 = address.getString();
  83.             String r4 = others.getString();
  84.             if (index < 0) {
  85.                 list.append(result, null);
  86.                 l1.append(r1, null);
  87.                 l2.append(r2, null);
  88.                 l3.append(r3, null);
  89.                 l4.append(r4, null);
  90.             } else {
  91.                 if (flag == cmdAppend) {
  92.                     list.insert(index + 1, result, null);
  93.                     l1.insert(index + 1, r1, null);
  94.                     l2.insert(index + 1, r2, null);
  95.                     l3.insert(index + 1, r3, null);
  96.                     l4.insert(index + 1, r4, null);
  97.                 } else {
  98.                     list.insert(index, result, null);
  99.                     l1.insert(index, r1, null);
  100.                     l2.insert(index, r2, null);
  101.                     l3.insert(index, r3, null);
  102.                     l4.insert(index, r4, null);
  103.                 }
  104.             }
  105.             display.setCurrent(list);
  106.         } else if (c == cmdOK2) {
  107.             display.setCurrent(list);
  108.         } else if (c == cmdSelect) {
  109.             int index = list.getSelectedIndex();
  110.             if (index < 0) {
  111.                 Alert alert = new Alert("通知");
  112.                 alert.setType(AlertType.INFO);
  113.                 alert.setTimeout(1000);
  114.                 alert.setString("没有记录");
  115.                 display.setCurrent(alert);
  116.             } else {
  117.                 int in = list.getSelectedIndex();
  118.                 name1.setString(l1.getString(in));
  119.                 phoneNo1.setString(l2.getString(in));
  120.                 address1.setString(l3.getString(in));
  121.                 others1.setString(l4.getString(in));
  122.                 display.setCurrent(editForm);
  123.             }
  124.         } else if (c == cmdDelete) {
  125.             int index = list.getSelectedIndex();
  126.             list.delete(index);
  127.             l1.delete(index);
  128.             l2.delete(index);
  129.             l3.delete(index);
  130.             l4.delete(index);
  131.         }
  132.         display.setCurrent(list);
  133.     }
  134.     }
 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值