您的位置:首页 > 其它

J2ME:模拟短信发送界面

2008-12-19 20:31 288 查看
 import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;

public class welcome extends MIDlet implements CommandListener,ItemStateListener{
   

 private Command cmdenter=new Command("写短信",Command.SCREEN,1);   //添加按钮
 private Command cmdexit=new Command("退出程序",Command.EXIT,1);
 private Command cmdclr=new Command("清除文本",Command.EXIT,1);
 private Command cmdback=new Command("返回",Command.EXIT,1);
 private Command cmdsend=new Command("发送",Command.SCREEN,1);
 private Command cmdconfirm=new Command("确认",Command.SCREEN,1);
 private ImageItem imgitem;             //图片项
 private Alert alert=new Alert("");    //提示窗口
 
 
 private Display dis;
 private Ticker ticker=new Ticker("欢迎使用写短信程序");   //走马灯
 private Form fwel=new Form("短息程序");      //表单
 private Form fedit=new Form("编辑短信");
 
 private TextField tfedit=new TextField("","",255,TextField.ANY);  //文本输入项
 
 private TextBox tbnum=new TextBox("编辑号码","",15,TextField.PHONENUMBER);  //文本输入,可以占据整个屏幕
 
 public welcome() throws IOException {
  // TODO Auto-generated constructor stub
  Image img=Image.createImage("/menu.png");
  imgitem=new ImageItem(null, img, Item.LAYOUT_CENTER|Item.LAYOUT_EXPAND, null);
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }

 protected void pauseApp() {
  // TODO Auto-generated method stub

 }

 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub
       dis=Display.getDisplay(this);
       dis.setCurrent(fwel);
       fwel.append(imgitem);
       fwel.addCommand(cmdexit);
       fwel.addCommand(cmdenter);
       fwel.setCommandListener(this);  //添加命令监听
       fwel.setTicker(ticker);
      
      
       tfedit.setLayout(Item.LAYOUT_EXPAND|Item.LAYOUT_CENTER);
       fedit.append(tfedit);
       fedit.addCommand(cmdback);
       fedit.addCommand(cmdsend);
      
       fedit.setCommandListener(this);
       fedit.setItemStateListener(this);   //添加状态监听
      
      /* fedit2.append(tfedit2);
       fedit2.addCommand(cmdclr);
       fedit2.addCommand(cmdsend);
       fedit2.setCommandListener(this);
       fedit2.setItemStateListener(this);
       */
      
       //tfedit.addCommand(cmdback);
       //tfedit.addCommand(cmdsend);
       //tfedit.setItemCommandListener(this);
      
      
       tbnum.addCommand(cmdback);
       tbnum.addCommand(cmdconfirm);
       tbnum.setCommandListener(this);
    
 }

 public void commandAction(Command c, Displayable d) {
  // TODO Auto-generated method stub   对命令事件的处理
  if(c.equals(cmdenter))
   dis.setCurrent(fedit);
  else if(c.equals(cmdexit))
  {try {
   this.destroyApp(true);
  } catch (MIDletStateChangeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  this.notifyDestroyed();}
  
  else if(c.equals(cmdback))
   {if(d.equals(fedit))
   dis.setCurrent(fwel);
   else if(d.equals(tbnum))
   dis.setCurrent(fedit);
   }
  else if(c.equals(cmdclr))
     {
      int pos=tfedit.getCaretPosition();
      tfedit.delete(--pos, 1);
      if(tfedit.size()==0)
      {fedit.removeCommand(cmdclr);
    fedit.addCommand(cmdback);}
         }
  
  else if(c.equals(cmdsend))
   dis.setCurrent(tbnum);
  
  else if(c.equals(cmdconfirm))
  {alert.setString("号码:"+tbnum.getString()+"/n"+"发送成功!");
   dis.setCurrent(alert, fwel);
   }
 }
 
 public void itemStateChanged(Item it) {
  // TODO Auto-generated method stub  对状态改变的处理
  if(it.equals(tfedit))
   {
   TextField tf=(TextField)it;
   if(tf.size()!=0)
   {
    fedit.removeCommand(cmdback);
    fedit.addCommand(cmdclr);
   }
   else
   {
    fedit.removeCommand(cmdclr);
    fedit.addCommand(cmdback);
   }
   }
 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息