论坛首页 移动开发技术论坛

How to use pop-up TextBox in Java ME

浏览 3397 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-05-23  

Overview

One of the Displayables in LCDUI is TextBox (extending Screen class), which allows user to enter and edit text. It is commonly used for entering relatively short texts, even single words. In any case TextBox has used the whole screen, which has made user experience bad. Now in S60 5th Edition new mode of pop-up TextBox is introduced. By using a JAD attribute "Nokia-UI-Enhancement" with value "PopUpTextBox" all the TextBox screens are shown as smaller dialogs, without obscuring the underlying screen.
Nokia-UI-Enhancement: PopUpTextBox

Pop-up TextBox does lack have some properties of "traditional" TextBox:
Ticker is not visible
Text input capacity indicator is not supported


An empty Pop-up TextBox has one line, but if needed, its size will grow. The exact maximum amount of visible lines depends on the screen size. In nHD screens (640x360 pixels) it is 5 rows of text. Inputting and editing text is possible by tapping on the TextBox.

The image below shows an empty pop-up TextBox on top of Canvas (in normal mode) and a pop-up TextBox with 5 rows of text

 

Here is a simple MIDlet demonstrating pop-up TextBox feature.
[edit]
Source code: PopUpTextBoxMIDlet.java
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
 
public class PopUpTextBoxMIDlet extends MIDlet {
    private PopUpTextBoxCanvas canvas;
    protected String canvasText = "Text from the TextBox";
 
    public void startApp() {
        canvas = new PopUpTextBoxCanvas(this);
        Display.getDisplay(this).setCurrent(canvas);
    }
 
    public void pauseApp() {
    }
 
    public void destroyApp(boolean unconditional) {
    }
 
    protected void closeTextBox(boolean update) {
        if (update) canvasText = canvas.textbox.getString();
        if (canvas.textbox != null) canvas.textbox = null;
        Display.getDisplay(this).setCurrent(canvas);
    }
 
    protected void showError(String title, String text) {
        Alert alert = new Alert(title, text, null, AlertType.ERROR);
        alert.setTimeout(Alert.FOREVER);
        alert.getType().playSound(Display.getDisplay(this));
        Displayable current = Display.getDisplay(this).getCurrent();
        if (current instanceof Alert) {}
        else Display.getDisplay(this).setCurrent(alert);
    }
}


[edit]
Source code: PopUpTextBox.java
import javax.microedition.lcdui.*;
 
public class PopUpTextBox extends TextBox implements CommandListener {
    private Command okCommand;
    private Command cancelCommand;
    private PopUpTextBoxMIDlet midlet;
   
    public PopUpTextBox(String title, String text, int maxsize, int constraints, PopUpTextBoxMIDlet midlet) {
        super(title, text, maxsize, constraints);
        this.midlet = midlet;
        okCommand = new Command("Ok", Command.OK, 1);
        cancelCommand = new Command("Cancel", Command.CANCEL, 1);
        this.addCommand(okCommand);
        this.addCommand(cancelCommand);
        this.setCommandListener(this);
    }
 
    public void commandAction(Command c, Displayable d) {
        if (c == okCommand) {
            midlet.closeTextBox(true);
        }
        if (c == cancelCommand) {
            midlet.closeTextBox(false);
        }
    }
}


[edit]
Source code: PopUpTextBoxCanvas.java
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextField;
 
public class PopUpTextBoxCanvas extends Canvas implements CommandListener {
    private PopUpTextBoxMIDlet midlet;
    private Command enterCommand;
    private Command exitCommand;
    protected PopUpTextBox textbox;
    private int width;
    private int height;
 
    public PopUpTextBoxCanvas(PopUpTextBoxMIDlet midlet) {
        this.midlet = midlet;
        enterCommand = new Command("Enter text", Command.SCREEN, 1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        this.addCommand(enterCommand);
        this.addCommand(exitCommand);
        this.setCommandListener(this);
    }
   
    public void paint(Graphics g) {
        g.setColor(255, 255,255);
        g.fillRect(0, 0, width, height);
        g.setColor(0, 0, 0);
        g.drawString(midlet.canvasText, 0, 0, Graphics.TOP|Graphics.LEFT);
    }
 
    protected  void keyPressed(int keyCode) { }
   
    protected  void keyReleased(int keyCode) { }
 
    protected  void keyRepeated(int keyCode) { }
   
    protected  void pointerDragged(int x, int y) { }
 
    protected  void pointerPressed(int x, int y) { }
 
    protected  void pointerReleased(int x, int y) { }
 
    protected void sizeChanged(int w, int h) {
        width = w;
        height = h;
        repaint();
    }
 
    public void commandAction(Command c, Displayable d) {
        if (c == enterCommand) {
            textbox = new PopUpTextBox("Enter text", midlet.canvasText, 1000, TextField.ANY, midlet);
            Display.getDisplay(midlet).setCurrent(textbox);
        }
        if (c == exitCommand) {
            midlet.notifyDestroyed();
        }
    }
}

 download http://www.forum.nokia.com/piazza/wiki/images/b/b3/PopUpTextBoxMIDlet.zip

 

论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics