正文

TextBox例子2006-08-05 20:11:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/17301.html

分享到:

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

//TextBox事例,made by sword2008
public class TextBoxDemo extends MIDlet implements CommandListener
{
//private
 private Display display;
 private ChoiceGroup types;
 private ChoiceGroup options;
 private Form mainform;
 private Command exitcmd=new Command("EXIT",Command.EXIT,1);
 private Command backcmd=new Command("BACK",Command.BACK,1);
 private Command screencmd=new Command("SCREEN",Command.SCREEN,1);
 private boolean firstTime;

//label
 String textlabels[]={"字","邮箱","NUMBRIC","DECIMAL","电话","网址",};
 int textBoxType[]={TextField.ANY,TextField.EMAILADDR,TextField.NUMERIC,
      TextField.DECIMAL,TextField.PHONENUMBER,TextField.URL};


  public TextBoxDemo(){
  display=Display.getDisplay(this);
  firstTime=true;
   }

         public void startApp(){
 if(firstTime)
 {mainform=new Form("选择一种textBox类型");
 mainform.append("以下为选项:");
 types=new ChoiceGroup("选择",Choice.EXCLUSIVE,textlabels,null);
 mainform.append(types);
 
//第二选项
 String optionsString[]={"密码形式","移动字符"};
 options=new ChoiceGroup("选择",Choice.MULTIPLE,optionsString,null);
 mainform.append(options);
 mainform.addCommand(screencmd);
 mainform.addCommand(exitcmd);
 mainform.setCommandListener(this);
 firstTime=false;
 }
  display.setCurrent(mainform);

  }
 
  public void pauseApp(){}
//按键监听
  public void commandAction(Command cmd,Displayable dis)
  {if(cmd==exitcmd){
  destroyApp(false);
  notifyDestroyed();
  }
   else if(cmd==screencmd){
  int index=types.getSelectedIndex();
  String title=textlabels[index];
  int choiceType=textBoxType[index];
  
  boolean flag[]=new boolean[2];
  options.getSelectedFlags(flag);
  if(flag[0]){
  choiceType |=TextField.PASSWORD;}
  TextBox textBox=new TextBox(title,"",50,choiceType);
  if(flag[1]){ 
  textBox.setTicker(new Ticker("TextBox"+title));}
  textBox.addCommand(backcmd);
  textBox.setCommandListener(this);
  display.setCurrent(textBox);
  
  }

   else if(cmd==backcmd){}

  }

  public void destroyApp(boolean unkowmn){}


}


/*注意:如果出现的错误:
Project settings saved
Building "TextBoxDemo"
f:\WTK22\apps\TextBoxDemo\src\TextBoxDemo.java:5: TextBoxDemo is not abstract and does not override abstract method commandAction(javax.microedition.lcdui.Command,javax.microedition.lcdui.Displayable) in javax.microedition.lcdui.CommandListener
public class TextBoxDemo extends MIDlet implements CommandListener
       ^
1 error
com.sun.kvem.ktools.ExecutionException
Build failed


一定是CommandAction出现问题,检查一下两个形式参数(Command cmd,Displayable dis)
注意是Displayable而不是Display
*/

 

阅读(3380) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册