Java Tutorial/J2ME/TextBox

Материал из Java эксперт
Перейти к: навигация, поиск

Add TextBox to a form

/* License
 * 
 * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  
 *  * Redistribution of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 * 
 *  * Redistribution in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *  
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *  
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility. 
 */
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
public class J2MEHelloMIDlet extends MIDlet implements CommandListener {
  // The exit command
  private Command exitCommand;
  // The display for this MIDlet
  private Display display;
  // create a ticker
  private Ticker hi = new Ticker("J2ME is cool");
  public J2MEHelloMIDlet() {
    display = Display.getDisplay(this);
    exitCommand = new Command("Exit", Command.SCREEN, 2);
  }
  public void startApp() {
    TextBox t = new TextBox("Hello MIDlet", "Wireless Internet", 256, 0);
    t.addCommand(exitCommand);
    t.setCommandListener(this);
    t.setTicker(hi); // set the ticker
    display.setCurrent(t);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}





Check the input value of a password field

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class LoginMidlet extends MIDlet implements CommandListener {
  private Display display;
  private TextField userName = new TextField("LoginID:", "", 10, TextField.ANY);
  private TextField password = new TextField("Password:", "", 10, TextField.PASSWORD);
  private Form form = new Form("Sign in");
  private Command cancel = new Command("Cancel", Command.CANCEL, 2);
  private Command login = new Command("Login", Command.OK, 2);
  public void startApp() {
    display = Display.getDisplay(this);
    form.append(userName);
    form.append(password);
    form.addCommand(cancel);
    form.addCommand(login);
    form.setCommandListener(this);
    display.setCurrent(form);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
    notifyDestroyed();
  }
  public void validateUser(String name, String password) {
    if (name.equals("name") && password.equals("pass")) {
      menu();
    } else {
      tryAgain();
    }
  }
  public void menu() {
    List services = new List("Choose one", Choice.EXCLUSIVE);
    services.append("Check Mail", null);
    services.append("Compose", null);
    services.append("Addresses", null);
    services.append("Options", null);
    services.append("Sign Out", null);
    display.setCurrent(services);
  }
  public void tryAgain() {
    Alert error = new Alert("Login Incorrect", "Please try again", null, AlertType.ERROR);
    error.setTimeout(Alert.FOREVER);
    userName.setString("");
    password.setString("");
    display.setCurrent(error, form);
  }
  public void commandAction(Command c, Displayable d) {
    String label = c.getLabel();
    if (label.equals("Cancel")) {
      destroyApp(true);
    } else if (label.equals("Login")) {
      validateUser(userName.getString(), password.getString());
    }
  }
}





Display text in TextBox

import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
public class TextBoxHelloWorld extends MIDlet implements CommandListener {
  private Display display;
  private TextBox textBox;
  private Command quitCommand;
  public void startApp() {
    display = Display.getDisplay(this);
    quitCommand = new Command("Quit", Command.SCREEN, 1);
    textBox = new TextBox("Hello World", "My first MIDlet", 40, 0);
    textBox.addCommand(quitCommand);
    textBox.setCommandListener(this);
    display.setCurrent(textBox);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command choice, Displayable displayable) {
    if (choice == quitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}





Get value from TextField

import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class J2METextFieldCapture extends MIDlet implements CommandListener {
  private Display display;
  private Form form = new Form("Sign In Please");
  private Command submit = new Command("Submit", Command.SCREEN, 1);
  private Command exit = new Command("Exit", Command.EXIT, 1);
  private TextField textfield = new TextField("First Name:", "", 30, TextField.ANY);
  public J2METextFieldCapture() {
    display = Display.getDisplay(this);
    form.addCommand(exit);
    form.addCommand(submit);
    form.append(textfield);
    form.setCommandListener(this);
  }
  public void startApp() {
    display.setCurrent(form);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command command, Displayable displayable) {
    if (command == submit) {
      textfield.setString("Hello, " + textfield.getString());
      form.removeCommand(submit);
    } else if (command == exit) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}





Numeric TextField

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class TextFieldNUMERIC extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Item Layout");
    form.append("line");
    form.append(new TextField("Number", "12345", 8, TextField.NUMERIC));
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





Phonenumber TextField

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class TextFieldPHONENUMBER extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Item Layout");
    form.append("line");
    form.append(new TextField("Phone", "1234567890", 10, TextField.PHONENUMBER));
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





Remove command from TextBox

import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class J2METextBoxCaptureRemove extends MIDlet implements CommandListener {
  private Display display;
  private TextBox textbox;
  private Command submit;
  private Command exit;
  public J2METextBoxCaptureRemove() {
    display = Display.getDisplay(this);
    submit = new Command("Submit", Command.SCREEN, 1);
    exit = new Command("Exit", Command.EXIT, 1);
    textbox = new TextBox("First Name:", "", 30, TextField.ANY);
    textbox.addCommand(exit);
    textbox.addCommand(submit);
    textbox.setCommandListener(this);
  }
  public void startApp() {
    display.setCurrent(textbox);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command command, Displayable displayable) {
    if (command == submit) {
      textbox.setString("Hello, " + textbox.getString());
      textbox.removeCommand(submit);
    } else if (command == exit) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}





Text Anchor

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class TextAnchorMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  Display display;
  public void startApp() {
    Display display = Display.getDisplay(this);
    Displayable d = new TextAnchorCanvas();
    exitCommand = new Command("exit", Command.EXIT, 1);
    d.addCommand(exitCommand);
    d.setCommandListener(this);
    display.setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
  }
}
class TextAnchorCanvas extends Canvas {
  int width = 0;
  int height = 0;
  public void paint(Graphics g) {
    width = getWidth();
    height = getHeight();
    g.setGrayScale(255);
    g.fillRect(0, 0, width - 1, height - 1);
    g.setGrayScale(0);
    g.drawRect(0, 0, width - 1, height - 1);
    g.drawString("TEXT", width / 2, height / 2, Graphics.BASELINE | Graphics.HCENTER);
  }
}





TextBox which accepts any input

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class TextFieldANY extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Item Layout");
    form.append("line");
    form.append(new TextField("Any", null, 8, TextField.ANY));
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





TextBox with size

import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
public class TextBoxSizeMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand = new Command("Exit", Command.BACK, 1);
  private Display display;
  public TextBoxSizeMIDlet() {
    display = Display.getDisplay(this);
  }
  public void startApp() {
    TextBox t = new TextBox("Hello MIDP", "Welcome to MIDP Programming", 256, 0);
    t.addCommand(exitCommand);
    t.setCommandListener(this);
    display.setCurrent(t);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}





TextField.PASSWORD

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class TextFieldPASSWORD extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Item Layout");
    form.append("line");
    form.append(new TextField("Password", null, 8, TextField.PASSWORD));
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





TextField.PASSWORD | TextField.NUMERIC)

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class TextFieldPASSWORDTextFieldNUMERIC extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Item Layout");
    form.append("line");
    form.append(new TextField("Password", null, 8, TextField.PASSWORD | TextField.NUMERIC));
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}