⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alerttest.java

📁 《Java ME手机应用开发大全》源码 書籍內容簡介: http://www.china-pub.com/410
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class AlertTest extends MIDlet implements CommandListener
{

    // 定义一个系统中所有Alert类的抽象父类
    public abstract class AlertRunner
    {
		//定义两个代表时间的常量值
        protected static final int ONE_SECOND = 1000;
        protected static final int FIVE_SECONDS = 5000;
        //声明对话框的标题
		private String title;
        public AlertRunner(String title)
        {
            this.title = title;
        }

        public abstract Alert doAlert();

        public String getTitle()
        {
            return title;
        }

        public String toString()
        {
            return getTitle();
        }     
    }

    // 定义定时消息对话框
    public class TimedAlert extends AlertRunner
    {
        public TimedAlert(String title)
        {
            super(title);
        }

        public Alert doAlert()
        {
            //创建一个对话框对象
			Alert a = new Alert(getTitle());
            a.setString("对话框将显示5秒");
			//设置对话框超时时间
            a.setTimeout(FIVE_SECONDS);
            //显示对话框
            showAlert(a);
            return a;
        }
    }

    // 定义模式消息对话框

    public class ModalAlert extends AlertRunner
    {
        public ModalAlert(String title)
        {
            super(title);
        }

        public Alert doAlert()
        {
			 //创建一个对话框对象
            Alert a = new Alert(getTitle());
            a.setString("单击Done按键,对话框返回");
			//设置对话框超时时间为FOREVER,则为模式对话框
            a.setTimeout(Alert.FOREVER);
            //显示对话框
            showAlert(a);
            return a;
        }
    }

    //定义带指示器的消息对话框

    public class ProgressAlert extends AlertRunner implements CommandListener,Runnable
    {

        private Alert alert;
		//声明代表当前进度的计数值
        private int counter;
		//声明指示器是否到头的判断标识
        private boolean done;
		//声明指示器对象
        private Gauge gauge;
		//声明指示器最大长度为10
        private static final int MAX = 10;
        public ProgressAlert(String title)
        {
            super(title);
        }

        public Alert doAlert()
        {
			//创建指示器实例
            gauge = new Gauge(null, false, MAX, 0);
			//设置指示器起始位置
            gauge.setValue(0);
			//判断标识为false
            done = false;
            //创建一个对话框对象
            alert = new Alert(getTitle());
            alert.setString("进度条结束后,单击Done软键对话框将消失");
            alert.setCommandListener(this);
			//将指示器添加到对话框上
            alert.setIndicator(gauge);
            // 设置一个非常大的超时时间
            alert.setTimeout(ONE_SECOND * 3600);
            //显示对话框
            showAlert(alert);
			//启动新线程
            new Thread(this).start();
            return alert;
        }

        public void commandAction(Command c, Displayable d)
        {
            //如果标识位位true或者指示器到头,则调用showList()方法
			if (done || gauge.getValue() >= MAX)
            {
                showList();
            }
        }

        private void done()
        {
            //向对话框中添加软键
			alert.addCommand(new Command("Done", Command.OK, 1));
            done = true;
        }

        // 定义指示器移动的线程方法
        public void run()
        {
            int val = gauge.getValue();
           //判断当前值是否超过10,如果没有则进程睡眠一段时间,当前值加1,循环之行,直到当前值超过10为止
            try
            {
                while (val < MAX)
                {
                    Thread.sleep(ONE_SECOND);
                    gauge.setValue(++val);
                }
            } catch (InterruptedException e)
            {
            }

            done();
        }

       
    }
    private Display display;
	private List alertList;
    private AlertRunner[] alertRunners;
    public static final Command exitCommand = new Command("Exit", Command.EXIT,1);

    public AlertTest()
    {
    }

    public void commandAction(Command c, Displayable d)
    {
        if (c == exitCommand)
        {
            exitMIDlet();
        } else if (c == List.SELECT_COMMAND)
        {
            int index = alertList.getSelectedIndex();
            alertRunners[index].doAlert();
        }
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException
    {
        exitMIDlet();
    }

    public void exitMIDlet()
    {
        notifyDestroyed();
    }

    public Display getDisplay()
    {
        return display;
    }

    protected void initMIDlet()
    {

        // 声明一个Alert数组,用来存放三种类型的Alert实例
        alertRunners = new AlertRunner[] { new TimedAlert("定时消息对话框"),
                new ModalAlert("模式消息对话框"),
                new ProgressAlert("带指示器的消息对话框")};
        //创建一个List对象
        alertList = new List("Alert Testing", List.IMPLICIT);
        alertList.setCommandListener(this);
        //将三种类型的Alert实例的字符串作为选项添加到List中
        for (int i = 0; i < alertRunners.length; ++i)
        {
            alertList.append(alertRunners[i].toString(), null);
        }
        showList();
    }

    protected void pauseApp()
    {
    }

    private void showAlert(Alert a)
    {
        getDisplay().setCurrent(a, alertList);
    }

    private void showList()
    {
        getDisplay().setCurrent(alertList);
    }

    protected void startApp() throws MIDletStateChangeException
    {
        if (display == null)
        {
            display = Display.getDisplay(this);
            initMIDlet();
        }
    }

    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -