PhoneEdit.java
上传用户:heyongping
上传日期:2022-07-01
资源大小:95k
文件大小:2k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * 创建于 2009-12-04
  3.  * 实现对电话号码的编辑
  4.  */
  5. package net.garrey.view;
  6. import javax.microedition.lcdui.Command;
  7. import javax.microedition.lcdui.CommandListener;
  8. import javax.microedition.lcdui.Displayable;
  9. import javax.microedition.lcdui.Form;
  10. import javax.microedition.lcdui.TextField;
  11. import net.garrey.model.PhoneItem;
  12. import net.garrey.util.UIController;
  13. /**
  14.  * 作者: 张三
  15.  * 编辑电话号码信息,实现对编辑保存按钮的监听
  16.  */
  17. public class PhoneEdit extends Form{
  18.     private UIController controller;
  19.     private Command BACK_COMMAND;
  20.     private Command SAVE_COMMAND;
  21.     private TextField nameField;
  22.     private TextField phoneField;
  23.     private PhoneItem item;
  24.     /*
  25.      * 带参数的构造方法
  26.      */
  27. public PhoneEdit(String title,UIController control){
  28. super(title);
  29.      controller=control;
  30.      this.setCommandListener(new PhoneEditListener());
  31.     
  32.      BACK_COMMAND=new Command("返回",Command.BACK,1);
  33.      SAVE_COMMAND=new Command("保存",Command.OK,2);
  34.      this.addCommand(BACK_COMMAND);
  35.      this.addCommand(SAVE_COMMAND);
  36.     
  37.      nameField=new TextField("名字","",15,TextField.ANY);
  38.      phoneField=new TextField("电话","",15,TextField.PHONENUMBER);
  39.      this.append(nameField);
  40.      this.append(phoneField);
  41. }
  42. /*
  43.  * 重新修改内容信息
  44.  */
  45. public void setPhone(PhoneItem item){
  46.     this.item=item;
  47.     nameField.setString(item.getName());
  48.     phoneField.setString(item.getPhonenum());
  49. }
  50.     //监听器
  51.     private class PhoneEditListener implements CommandListener{ 
  52.      public void commandAction(Command command, Displayable disp){
  53.      if(command==BACK_COMMAND){
  54.      controller.handleEvent(UIController.EventID.EVENT_EDIT_BACK,null);
  55.              }else if(command==SAVE_COMMAND){
  56.                  String name=nameField.getString();
  57.                  String phone=phoneField.getString();
  58.                  if((name==null||name.equals(""))||(phone==null||phone.equals(""))){
  59.                      return;
  60.                  }
  61.                
  62.                  item.setName(name);
  63.                  item.setPhonenum(phone);
  64.      Object[] args={item};
  65.      controller.handleEvent(UIController.EventID.EVENT_EDIT_SAVE,args);                                      
  66.      }//end else
  67.      }
  68.     }//end inner class
  69.     
  70. }