繁体   English   中英

如何在Java ME中向用户询问一些数字?

[英]How can I ask the user for some numbers in Java ME?

我是Java Me的新手(第一次)。 我希望我的程序向用户询问IP地址。 因此,四个数字介于0到255之间。这并不困难,但是正如我所说,我是Java Me的新手。

您希望用户如何输入IP地址? 您是否要使用TextField例如? 您可以执行以下操作:

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

public class GetIPAddress extends MIDlet implements CommandListener {

  private Display display;
  private Form form = new Form("IP Adress Input");
  private Command submit = new Command("Submit", Command.SCREEN, 1);
  private Command exit = new Command("Exit", Command.EXIT, 1);

  private TextField textfield = new TextField("IP Address:", "", 30, TextField.ANY);

  public GetIPAddress() {
    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) {
      // Do the manipulation of the IP address here. 
      // You can retrieve it using textfield.getString();
      form.removeCommand(submit);
    } else if (command == exit) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM