正文

[转]第3章  MIDP高级UI 的使用22006-08-05 08:55:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/17267.html

分享到:

3.4    Alert         

这个类比较有意思,它是用来提醒用户关于错误或者其他异常情况的屏幕对象,这个警告只能作为简短的信息记录和提醒,如果我们需要长一点的,我们可以使用其它的Screen子类,最常见的是Form。同时我们顺便提一下跟它相关的一个类AlertType,需要提醒读者注意的一点是AlertType是一个本身无法实体化的工具类。(即我们不能象Form那样产生具体的对象)

 

AlertType共有5个类型:ALARM(警报),CONFIRMATION(确定),ERROR(错误),INFO(信息提示),WARNING(警告)。

 

Alert是一个比较特殊的屏幕对象,当我们在setCurrent()方法中调用它的时候,它会先发出一段警告的声音,然后彩绘显示在屏幕上,过了一段时间之后,它会自动跳回之前的画面。

 

我们需要注意的是我们必须在使用setCurrent()显示Alert之前定义好它可以跳回的画面,否则会发生异常。

 

Alert中我们可以通过setTimeout()方法来设定间隔的时间,setType()来调用我们上面提到的四种类型,setImage()来定义图片,setString()来定义内含文字,同时通过getType(),getImage(),getString()来取得相应的对象。

 

Alert显示了我们在setTimeout()中指定的间隔时间后,它会跳回我们之前指定的对象,如果我们在指定显示时间时传入了Alert.FOREVER作为参数,这时,除非用户按下定义哈哦的接触键,否则,屏幕会一直显示这个Alert。如果在一个定时的Alert中只有一个命令,那么超时发生时命令会自动激活。

 

import javax.microedition.lcdui.*;

import javax.microedition.midlet.MIDlet;

 

 

public class AlertDemo

    extends MIDlet {

 

    private final static Command CMD_EXIT = new Command("Exit", Command.EXIT,

                                                        1);

    private final static Command CMD_SHOW = new Command("Show", Command.SCREEN,

                                                        1);

    private final static String[] typeStrings = {

            "Alarm", "Confirmation", "Error", "Info", "Warning"

        };

    private final static String[] timeoutStrings = {

            "2 Seconds", "4 Seconds", "8 Seconds", "Forever"

        };

    private final static int SECOND = 1000;

    private Display display;

   

    private boolean firstTime;

   

    private Form mainForm;

   

    public AlertDemo() {

        firstTime = true;

        mainForm = new Form("Alert Options");

    }

   

    protected void startApp() {

        display = Display.getDisplay(this);

        showOption();

    }

 

    /**

     * 制造这个MIDlet的基本显示

     * 在这个Form里面我们可以选择Alert的个中类型和特性

     */

    private void showOption() {

        if(firstTime) {               

            // choice-group for the type of the alert:

            // "Alarm", "Confirmation", "Error", "Info" or  "Warning"

            ChoiceGroup types = new ChoiceGroup("Type", ChoiceGroup.POPUP,

                                                typeStrings, null);

            mainForm.append(types);

           

            // choice-group for the timeout of the alert:

            // "2 Seconds", "4 Seconds", "8 Seconds" or "Forever"

            ChoiceGroup timeouts = new ChoiceGroup("Timeout", ChoiceGroup.POPUP,

                                                   timeoutStrings, null);

            mainForm.append(timeouts);

   

            // a check-box to add an indicator to the alert

            String[] optionStrings = { "Show Indicator" };

            ChoiceGroup options = new ChoiceGroup("Options", Choice.MULTIPLE,

                                                  optionStrings, null);

            mainForm.append(options);

            mainForm.addCommand(CMD_SHOW);

            mainForm.addCommand(CMD_EXIT);

            mainForm.setCommandListener(new AlerListener(types, timeouts, options));

            firstTime = false;

        }

        display.setCurrent(mainForm);

    }

 

    private class AlerListener

        implements CommandListener {

 

        AlertType[] alertTypes = {

            AlertType.ALARM, AlertType.CONFIRMATION, AlertType.ERROR,

            AlertType.INFO, AlertType.WARNING

        };

        ChoiceGroup typesCG;

        int[] timeouts = { 2 * SECOND, 4 * SECOND, 8 * SECOND, Alert.FOREVER };

        ChoiceGroup timeoutsCG;

        ChoiceGroup indicatorCG;

 

        public AlerListener(ChoiceGroup types, ChoiceGroup timeouts,

                            ChoiceGroup indicator) {

            typesCG = types;

            timeoutsCG = timeouts;

            indicatorCG = indicator;

        }

 

        public void commandAction(Command c, Displayable d) {

 

            if (c == CMD_SHOW) {

 

                int typeIndex = typesCG.getSelectedIndex();

                Alert alert = new Alert("Alert");

                alert.setType(alertTypes[typeIndex]);

 

                int timeoutIndex = timeoutsCG.getSelectedIndex();

                alert.setTimeout(timeouts[timeoutIndex]);

                alert.setString(

                        typeStrings[typeIndex] + " Alert, Running " + timeoutStrings[timeoutIndex]);

 

                boolean[] SelectedFlags = new boolean[1];

                indicatorCG.getSelectedFlags(SelectedFlags);

 

                if (SelectedFlags[0]) {

 

                    Gauge indicator = createIndicator(timeouts[timeoutIndex]);

                    alert.setIndicator(indicator);

                }

 

                display.setCurrent(alert);

            } else if (c == CMD_EXIT) {

                destroyApp(false);

                notifyDestroyed();

            }

        }

    }

 

    protected void destroyApp(boolean unconditional) {

    }

 

    protected void pauseApp() {

    }

 

    /**

     * 我们在这里生成Alertindicator.

     * 如果这里没有timeout, 那么这个indicator 将是一个 "非交互性的" gauge

     *  用有一个后台运行的thread更新.

     */

    private Gauge createIndicator(int maxValue) {

 

        if (maxValue == Alert.FOREVER) {

 

            return new Gauge(null, false, Gauge.INDEFINITE,

                             Gauge.CONTINUOUS_RUNNING);

        }

 

        final int max = maxValue / SECOND;

        final Gauge indicator = new Gauge(null, false, max, 0);

 

//        if (maxValue != Gauge.INDEFINITE) {

           

            new Thread() {

                public void run() {

 

                    int value = 0;

 

                    while (value < max) {

                        indicator.setValue(value);

                        ++value;

 

                        try {

                            Thread.sleep(1000);

                        } catch (InterruptedException ie) {

                            // ignore

                        }

                    }

                }

            }.start();

      }

 

        return indicator;

    }

}              

3.5    Form概述

FormJ2ME里面一个比较重要的容器类型,可以说是集中了高级UI中的精华,是开发当中常常用到的一个关键类,下图很好的说明了FORM及其相关子类的关系: 

                          

 

我们通常是往Form里面添加个中Item的子类(使用append()方法),从而达到让画面更加丰富的目的,每一个Item的子类在同一时刻只能属于同一个容器,否则会引发异常。

 

Form画面中,我们通过Item.LAYOUT_LEFTItem.LAYOUT_CENTERItem.LAYOUT_RIGHT来控制各个ItemForm的位置,通过这几个参数的字面意思我们很容易明白分别是左,中,右。在不设定的情况下,Item会依照LAYOUT_DEFAULT来绘制,如果我们希望自己来设定等效线,可以用setLayout()这个方法来控制。

同时,Form缺省的设定会在空间足够的情况下,尽可能让Item出现在同一个逻辑区域中。如果组件在显示时,比我们预期的最大的尺寸要大(或比预期最小尺寸更小),那么系统会自动忽略我们之前的设定,转而采用最大尺寸或者最小尺寸,这时系统会自动调用setPreferredSize(),将预期尺寸设置好。

 

3.6    StringItemImageItem

3.6.1   StringItem

StringItem的作用,从它字面上意思来看就可以很明白,就是在屏幕上显示一串字,配合不同的外观类型, StringItem有两个构造函数,最长见的是需要三个参数的,第一个是Label,第二个是内容,第三个则是外观,外观共分三种:PLAIN,BUTTON,HYPERLINK,(只需两个参数的构造函数等同于使用PLAIN的外观的三个参数的构造函数),对于外观的提取,我们可以使用getAppearanceMode()取得,以此类推,需要修改/得到相应的参数只需进行相应的set/get操作即可。

 

 

我们可以把Item和其他的高级UI部分结合起来,这样也是对我们学习的一种促进:

 

import javax.microedition.lcdui.*;

import javax.microedition.midlet.MIDlet;

 

 

public class StringItemDemo

 extends MIDlet

implements CommandListener, ItemCommandListener {

 

    private Display display;

    private Form mainForm;

    private final static Command CMD_GO = new Command("Go", Command.ITEM, 1);

    private final static Command CMD_PRESS = new Command("Press", Command.ITEM, 1);

    private final static Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);

 

   protected void startApp() {

        display = Display.getDisplay(this);

 

        mainForm = new Form("String Item Demo");

        mainForm.append("This is a simple label");

 

        StringItem item = new StringItem("This is a StringItem label: ",

                                         "This is the StringItems text");

        mainForm.append(item);

        item = new StringItem("Short label: ", "text");

        mainForm.append(item);

        item = new StringItem("Hyper-Link ", "hyperlink", Item.HYPERLINK);

        item.setDefaultCommand(CMD_GO);

        item.setItemCommandListener(this);

        mainForm.append(item);

        item = new StringItem("Button ", "Button", Item.BUTTON);

        item.setDefaultCommand(CMD_PRESS);

        item.setItemCommandListener(this);

        mainForm.append(item);

        mainForm.addCommand(CMD_EXIT);

        mainForm.setCommandListener(this);

        display.setCurrent(mainForm);

    }

 

    public void commandAction(Command c, Item item) {

       if (c == CMD_GO) {

            String text = "Go to the URL...";

            Alert a = new Alert("URL", text, null, AlertType.INFO);

            display.setCurrent(a);

        } else if (c == CMD_PRESS) {

            String text = "Do an action...";

            Alert a = new Alert("Action", text, null, AlertType.INFO);

            display.setCurrent(a);

        }

    }

 

    public void commandAction(Command c, Displayable d) {

            destroyApp(false);

            notifyDestroyed();       

    }

   

 

    protected void destroyApp(boolean unconditional) {

    }

 

 

       protected void pauseApp() {

      }           

}

阅读(2737) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册