和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 *** 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 *** 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 *** 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);
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;
}