How to use pop-up TextBox in Java ME

来源:未知 责任编辑:智问网络 发表时间:2013-11-04 19:49 点击:

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

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Java编程语言的八大优点
    • JVM对象生命周期详细介绍
    • Java平台上的CRM系统
    • Java 算数测试小程序
    • Command(命令模式)
    • Java环境 使用Resin在NT环境下配置JSP环境
    • Java 一个简单的画图程序
    • Java 日历的小程序
    • Java 统计代码的小工具
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1