Java Tutorial/J2ME/CommandListener

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

Add CommandListener to 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 CommandQuitGoodbyeWorld 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("Goodbye World", "My second 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();
    }
  }
}





Use CommandListener

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 J2MECommander extends MIDlet {
  public void startApp() {
    Displayable d = new TextBox("TextBox", "Commander", 20, TextField.ANY);
    Command c = new Command("Exit", Command.EXIT, 0);
    d.addCommand(c);
    d.setCommandListener(new CommandListener() {
      public void commandAction(Command c, Displayable s) {
        notifyDestroyed();
      }
    });
    Display.getDisplay(this).setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
}