alert类继承自screen,它的作用是通知用户发生异常信息。在高级用户界面api中有三个类是不能有父容器的,他们必须要占据屏幕,其中一个就是alert,另外两个是list和textbox。在midp1.0中,alert的灵活性比较差,表示的信息比较死板。在midp2.0中添加了一个重要的特性就是能够在alert上添加指示器,因此可以和guage结合起来使用。
和alert结合使用的guage有以下要求1.必须是非交互性的,2.不能同时在其他的容器内,3.guage的label必须为null,4.guage不能和command和commandlistener关联。比如按照如下方式创建一个guage实例
int max = ... // maximum value
int initial = ... // initial value
gauge gauge = new gauge( null, false, max, initial );
通过调用方法setindicator()可以把alert和guage结合起来
alert a = new alert( "my alert" );
a.setindicator( gauge );
由于midp的用户界面类都是线程安全的,因此你可以在其他的线程内改变guage的值,alert会自动的重新绘制屏幕来更新guage的当前值。
在midp2.0中,我们可以显式的对alert添加command,当然如果你还要实现commandlistener的commandaction()方法来告诉系统当command被按下的时候该做什么。在midp1.0中,有个默认的command和alert关联在一起的,如果我们在alert中显式的添加了command的话,那么这个默认的command就被取代,如果添加的command被删除,默认的command会自动恢复和alert的关联。如果我们在alert上添加了两个以上的command,那么它的timeout会自动设置为forever。
下面的应用程序很好的说明了如何使用不同的alert表示不同的信息
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
// a simple midlet for testing various alerts.
public class alerttest extends midlet implements commandlistener
{
// an abstract class for our alert tests.
public abstract class alertrunner
{
protected static final int one_second = 1000;
protected static final int five_seconds = 5000;
public alertrunner(string title)
{
_title = title;
}
public abstract alert doalert();
public string gettitle()
{
return _title;
}
public string tostring()
{
return gettitle();
}
private string _title;
}
// an alert test for a simple timed alert.
public class timedalert extends alertrunner
{
public timedalert(string title)
{
super(title);
}
public alert doalert()
{
alert a = new alert(gettitle());
a.setstring("times out after 5 seconds...");
a.settimeout(five_seconds);
showalert(a);
return a;
}
}
// an alert test for a simple modal alert.
public class modalalert extends alertrunner
{
public modalalert(string title)
{
super(title);
}
public alert doalert()
{
alert a = new alert(gettitle());
a.setstring("waits to be dismissed");
a.settimeout(alert.forever);
showalert(a);
return a;
}
}
// an alert test that displays an alert with
// a gauge whose value is increased every second.
// the alert can be dismissed only after the gauge
// reaches its maximum value.
public class progressalert extends alertrunner implements commandlistener,
runnable
{
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);
_done = false;
_alert = new alert(gettitle());
_alert.setstring("counts to " + max
+ " and then lets you dismiss it");
_alert.setcommandlistener(this);
_alert.setindicator(_gauge);
// set a _very_ long timeout
_alert.settimeout(one_second * 3600);
showalert(_alert);
new thread(this).start();
return _alert;
}
public void commandaction(command c, displayable d)
{
if (_done || _gauge.getvalue() >= max)
{
showlist();
}
}
private void done()
{
_alert.addcommand(new command("done", command.ok, 1));
_done = true;
}
// a thread that bumps the value of the counter
// every second.
public void run()
{
int val = _gauge.getvalue();
try
{
while (val < max)
{
thread.sleep(one_second);
_gauge.setvalue(++val);
}
} catch (interruptedexception e)
{
}
done();
}
private alert _alert;
private int _counter;
private boolean _done;
private gauge _gauge;
}
// an alert test that displays a continuously
// running gauge before automatically timing out.
public class busyalert extends alertrunner
{
public busyalert(string title)
{
super(title);
}
public alert doalert()
{
_gauge = new gauge(null, false, gauge.indefinite,
gauge.continuous_running);
_alert = new alert(gettitle());
_alert.setstring("runs for 5 seconds and "
+ "times out automatically");
_alert.setindicator(_gauge);
_alert.settimeout(five_seconds);
showalert(_alert);
return _alert;
}
private alert _alert;
private gauge _gauge;
}
// standard midlet code. displays a list of
// available alert tests and runs the test once
// it's been chosen.
private display display;
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()
{
// the list of alert tests....
_alertrunners = new alertrunner[] { new timedalert("timed alert"),
new modalalert("modal alert"),
new progressalert("progress alert"),
new busyalert("busy alert") };
_alertlist = new list("alert testing", list.implicit);
_alertlist.setcommandlistener(this);
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();
}
}
private list _alertlist;
private alertrunner[] _alertrunners;
}
闽公网安备 35060202000074号