程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> MIDP高級UI的使用(三)TextBox

MIDP高級UI的使用(三)TextBox

編輯:關於JAVA

當我們需要再移動設備上輸入數據時,TextBox 就派上用場了,我們使用的TextBox 構造函數參數共有四個,TextBox textbox = new TextBox(string title, string content, string maxLength, string limitType) ,第一個是標題,第二個是TextBox 的初始內容,第三個是允許輸入字符的最大長度,第四個是限制內型。值得注意的是:一個TextBox 必須附加一個命令,否則用戶將不能激發任何行為,而陷入這個TextBox 中。

下面是一個常見的TextBox 的例子。

view plainprint?
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thinkrace.TextBox;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.*;
/**
 * @author pengjw
 */
public class TextBoxDemo extends MIDlet implements CommandListener{
  private Display display;
  private ChoiceGroup types;
  private ChoiceGroup options;
  private Form mainForm;
  private static final Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
  private static final Command CMD_BACK = new Command("Back", Command.BACK, 1);
  private static final Command CMD_SHOW = new Command("Show", Command.SCREEN ,1);
  private static final String[] textBoxLabels ={
    "Any Character", "Email", "Number", "Decimal", "Phone", "URL"
  };
  private static final int[] textBoxTypes ={
    TextField.ANY, TextField.EMAILADDR, TextField.NUMERIC,
    TextField.DECIMAL, TextField.PHONENUMBER, TextField.URL
  };
  //firstTime用來判斷TextBoxDemo是否被實例化 
  private boolean firstTime;
  public TextBoxDemo(){
    /**
     * 來自文檔的說明 
     * Display represents the manager of the display and input devices of the system. 
     * It includes methods for retrieving properties of the device and for requesting that objects be displayed on the device. 
     * There is exactly one instance of Display per MIDlet and the application can get 
     * a reference to that instance by calling the getDisplay() method. 
     */
    display = Display.getDisplay(this);
    firstTime = true;
  }
  public void startApp() {
    if(firstTime){
      mainForm = new Form("Select a Text Box Type");
      mainForm.append("select a textbox type");
      types = new ChoiceGroup("Choice Type", Choice.EXCLUSIVE, textBoxLabels, null);
      mainForm.append(types);
      String[] optionStrings = {"As Password", "Show Ticker"};
      options = new ChoiceGroup("Options", Choice.MULTIPLE, optionStrings, null);
      mainForm.append(options);
      mainForm.addCommand(CMD_EXIT);
      mainForm.addCommand(CMD_SHOW);
      mainForm.setCommandListener(this);
      firstTime = false;
    }
    //設置當前顯示的窗體
    display.setCurrent(mainForm);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  /**
   * 實現CommandListener接口的抽象方法
   * 根據不同的Command做不同的事情
   */
  public void commandAction(Command c, Displayable d) {
    if(c == CMD_EXIT){
      destroyApp(false);
      notifyDestroyed();
    }
    else if(c == CMD_SHOW){
      int Index = types.getSelectedIndex();
      String title = textBoxLabels[Index];
      int choiceType = textBoxTypes[Index];
      boolean[] flags = new boolean[2];
      /** 
       * 來自文檔的說明,這個方法可以給一個bool數組賦值 
       * Query the state of a choicegroup and renturns the state of all elements in the boolean array. 
       */
      options.getSelectedFlags(flags);
      if(flags[0]){
        choiceType = TextField.PASSWORD;
      }
      TextBox textBox = new TextBox(title,"",50,choiceType);
      if(flags[1]){
        textBox.setTicker(new Ticker("TextBox:" + title));
      }
      textBox.addCommand(CMD_BACK);
      textBox.setCommandListener(this);
      display.setCurrent(textBox);
    }
    else if(c == CMD_BACK){
      display.setCurrent(mainForm);
    }
  }
} 

附上兩張效果圖:

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved