TextBoxDemo.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import javax.microedition.midlet.MIDlet;
  3. public class TextBoxDemo
  4.     extends MIDlet
  5.     implements CommandListener {
  6.     private Display display;
  7.     private ChoiceGroup types;
  8.     private ChoiceGroup options;
  9.     private Form mainForm;
  10.     private final static Command CMD_EXIT = new Command("Exit",Command.EXIT, 1);
  11.     private final static Command CMD_BACK = new Command("Back",Command.BACK, 1);
  12.     private final static Command CMD_SHOW = new Command("Show", Command.SCREEN,1);
  13.     static final String[] textBoxLabels = {
  14.         "Any Character", "E-Mail", "Number", "Decimal", "Phone", "Url"
  15.     };
  16.     static final int[] textBoxTypes = {
  17.         TextField.ANY, TextField.EMAILADDR, TextField.NUMERIC, 
  18.         TextField.DECIMAL, TextField.PHONENUMBER, TextField.URL
  19.     };
  20.     private boolean firstTime;
  21.     public TextBoxDemo() {
  22.         display = Display.getDisplay(this);
  23.         firstTime = true;
  24.     }
  25.     protected void startApp() {
  26.         if (firstTime) {
  27.             mainForm = new Form("Select a Text Box Type");
  28.             mainForm.append("TextBox演示");
  29.             Image[] imageArray = null;
  30.             types = new ChoiceGroup("Choose type", Choice.EXCLUSIVE, 
  31.                                                 textBoxLabels, imageArray);
  32.             mainForm.append(types);
  33.             String[] optionStrings = { "As Password", "Show Ticker" };
  34.             options = new ChoiceGroup("Options", Choice.MULTIPLE, 
  35.                                                   optionStrings, null);
  36.             mainForm.append(options);
  37.             mainForm.addCommand(CMD_SHOW);
  38.             mainForm.addCommand(CMD_EXIT);
  39.             mainForm.setCommandListener(this);
  40.             firstTime = false;
  41.         }
  42.         display.setCurrent(mainForm);
  43.     }
  44.     protected void destroyApp(boolean unconditional) {
  45.     }
  46.     protected void pauseApp() {
  47.     }
  48.     public void commandAction(Command c, Displayable d) {
  49.         if (c == CMD_EXIT) {
  50.             destroyApp(false);
  51.             notifyDestroyed();
  52.         } else if (c == CMD_SHOW) {
  53.             // these are the images and strings for the choices.
  54.             Image[] imageArray = null;
  55.             int index = types.getSelectedIndex();
  56.             String title = textBoxLabels[index];
  57.             int choiceType = textBoxTypes[index];            
  58.             boolean[] flags = new boolean[2];            
  59.             options.getSelectedFlags(flags);
  60.             if (flags[0]) {
  61.                 choiceType |= TextField.PASSWORD;
  62.             }
  63.             TextBox textBox = new TextBox(title, "", 500, 
  64.                                            choiceType);
  65.             if (flags[1]) {
  66.                 textBox.setTicker(new Ticker("TextBox: " + title));
  67.             }
  68.             textBox.addCommand(CMD_BACK);
  69.             textBox.setCommandListener(this);
  70.             display.setCurrent(textBox);
  71.         } else if (c == CMD_BACK) {
  72.             display.setCurrent(mainForm);
  73.         }
  74.     }
  75. }