当前位置:天才代写 > tutorial > JAVA 教程 > MIDP高级UI的利用(四)Alert

MIDP高级UI的利用(四)Alert

2017-11-12 08:00 星期日 所属: JAVA 教程 浏览:347

副标题#e#

这个类较量有意思,它是用来提醒用户关于错误可能其他异常环境的屏幕工具,这个告诫只能作为简短的信息记录和提醒,假如我们需要长一点的,我们可以利用其它的Screen 子类,最常见的是Form 。同时我们顺便提一下和它相关的一个类AlertType ,需要提醒读者留意的一点是AlertType 是一个自己无法实例化的东西类。(即我们不能像Form 那样发生详细工具)

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

Alert 是一个较量非凡的屏幕工具,当我们在setCurrent() 要领中挪用它的时候,它会先发出一段告诫的声音,然后才会显示在屏幕上,过了一段时间后,它会自动跳回之前的画面。

我们需要留意的是我们必需在利用setCurrent() 显示Alert 之前界说好它可以跳回的画面,不然会产生异常。

在Alert 中我们可以通过setTimeout() 要领来设定隔断的时间,setType() 来挪用上面提到的几种范例,setImage() 来界说图片,setString() 来界说内含文字,同时通过getType() ,getImage() ,getString() 来取得相应的工具。

可以操作setTimeout() 来界说Alert() 显示的时间,当Alert 在屏幕上显示了我们指定的时距离断后,它会跳回我们指定的屏幕工具,或回到前一个屏幕。假如我们挪用setTimeout() 时传入Alert.FORVEER 作为参数,那么除非用户按下指定键,不然屏幕会一直显示这个Alert 。假如在一个按时的Alert 中只有一个呼吁,那么超时产生时呼吁会自动激活。


#p#副标题#e#

下面是一个简朴的Alert例子:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */  

package com.thinkrace.Alert;
  
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.midlet.*;

/**
 * @author pengjw 
 */
public class AlertDemo extends MIDlet {
  private static final Command CMD_EXIT = new Command("Exit", Command.EXIT,1);
   private static final Command CMD_SHOW = new Command("Show", Command.SCREEN,1);
   private static final String[] typeStrings ={
    "Alarm", "Confirmation", "Error", "Info", "Warning"
   };
   private static final String[] timeoutStrings ={
    "2 seconds", "4seconds", "8 seconds", "Forever"
   };
   private static final int SECOND = 1000;
   private Display display;
   private boolean firstTime;
   private Form mainForm;
  
   public AlertDemo(){
     firstTime = true;
     mainForm = new Form("Alert Options");
   }
  public void startApp() {
    display = Display.getDisplay(this);
    showOption();
  }
  /**
   * 制造这个MIDlet的根基显示
   * 在这个Form内里我们可以选择Alert的各类范例和特性
   */
  private void showOption(){
    if(firstTime){
      /**
       * 来自文档的说明,ChoiceGroup类就是选项组,可以按照它的范例来抉择它是单选照旧多选,很像List类
       * A ChoiceGroup is a group of selectable elements intended to be placed within a Form.
       * The group may be created with a mode that requires a single choice to be made or that allows multiple choices. 
       * 以下用的是这种结构函数来建设ChoiceGroup的
       * public ChoiceGroup(String label,
          int choiceType,
          String[] stringElements,
          Image[] imageElements)
       */
      ChoiceGroup types = new ChoiceGroup("Type",ChoiceGroup.POPUP,typeStrings,null);
      mainForm.append(types);
      ChoiceGroup timeouts = new ChoiceGroup("Timeout",ChoiceGroup.POPUP,timeoutStrings,null);
      mainForm.append(timeouts);
      String[] optionStrings = {"Show Indicator"};
      ChoiceGroup options = new ChoiceGroup("Options", ChoiceGroup.MULTIPLE, optionStrings,null);
      mainForm.append(options);
      mainForm.addCommand(CMD_SHOW);
      mainForm.addCommand(CMD_EXIT);
      mainForm.setCommandListener(new AlertListener(types, timeouts, options));
      firstTime = false;
    }
    //配置当前显示的窗体
    display.setCurrent(mainForm);
  }

  //声明一个内部类,担任CommandListener接口
  private class AlertListener 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 AlertListener(ChoiceGroup types, ChoiceGroup timeouts, ChoiceGroup indicator){
      typesCG = types;
      timeoutsCG = timeouts;
      indicatorCG = indicator;
    }
  
    //实现CommandListener接口中的抽象要领,按拍照应的Command工具,表示相应的行为
    public void commandAction(Command c, Displayable d) {
      if(c == CMD_SHOW){
        //获取ChoiceGroup的选择项  
        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];
        //getSelectedFlags()可以或许获取ChoiceGroup中的每一个元素是否被选中,0代表未选中,1代表选中,它返回一个bool数组
        indicatorCG.getSelectedFlags(SelectedFlags);
  
        //当选中Show Indicator时,会有一个进度条来显示Alert存在的时间
        if(SelectedFlags[0]){
          Gauge indicator = createIndicator(timeouts[timeoutIndex]);
          alert.setIndicator(indicator);
        }
        display.setCurrent(alert);
      }
      else if(c == CMD_EXIT){
        destroyApp(false);
        notifyDestroyed();
      }
    }

  }
  
  private Gauge createIndicator(int maxValue){
    /**
     * Implements a graphical display, such as a bar graph, of an integer value.
     * The Gauge contains a current value that lies between zero and the maximum value, inclusive.
     * The application can control the current value and maximum value.
     * The range of values specified by the application may be larger than the number of distinct visual states possible on the device, 
     * so more than one value may have the same visual representation. 
     * Gauge(String label, boolean interactive, int maxValue, int initialValue)
     */
    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 (Exception e) {
            }
          }
        }
      }.start();
    }
    return indicator;
  }
  
  public void pauseApp() {
  }
  
  public void destroyApp(boolean unconditional) {
  }
}

#p#分页标题#e#

#p#副标题#e#

MIDP高级UI的操作(四)Alert
MIDP高级UI的操作(四)Alert

未选中Show Indicator时的结果图:

MIDP高级UI的操作(四)Alert

选中Show Indicator时的结果图:

MIDP高级UI的操作(四)Alert

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式