midp api 尽管维护的是一个受限的框架,但它还是提供了 ui 元素的完整集合。以下是最重要的 ui 元素中的一些:
alert 用于在屏幕上向用户显示关于异常情况或错误的信息。
choice 用于实现从既定数量的选项中进行选择。
choicegroup 提供一组相关选项。
form 作为其它 ui 元素的容器。
list 提供一个选项列表。
stringitem 充当只显(display-only)字符串之用。
textbox 是允许用户输入和编辑文本的屏幕显示。
textfield 允许用户输入和编辑文本。多个 textfield 可放到一个 form 中。
datefield 是一个可编辑的组件,用于表示日期和时间信息。datefield 可以放到 form 中。
ticker 用于文本的可滚动显示。
一个样本应用程序:电话日历
j2me 的著名特色之一是它在受限环境中的日期处理功能。j2me 提供的 datefield ui 元素是一个可编辑的组件,该组件用于表示日历信息(即日期和时间)。在这一部分中,我们将使用 datefield 和 date 函数来开发一个 j2me 应用程序,这个应用程序用于在移动电话 ui 上显示一个滚动日历。
// import of api classes
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
//a first midlet with simple text and a few commands.
public class phonecalendar extends midlet
implements commandlistener, itemstatelistener {
//the commands
private command exitcommand;
//the display for this midlet
private display display;
// display items e.g form and datefield
form displayform;
datefield date;
public phonecalendar() {
display = display.getdisplay(this);
exitcommand = new command("exit", command.screen, 1);
date = new datefield("select to date", datefield.date);
}
// start the midlet by creating the form and
// associating the exit command and listener.
public void startapp() {
displayform = new form("quick calendar");
displayform.append(date);
displayform.addcommand(exitcommand);
displayform.setcommandlistener(this);
displayform.setitemstatelistener(this);
display.setcurrent(displayform);
}
public void itemstatechanged(item item)
{
// get the values from changed item
}
// pause is a no-op when there is no background
// activities or record stores to be closed.
public void pauseapp() { }
// destroy must cleanup everything not handled
// by the garbage collector.
public void destroyapp (boolean unconditional) { }
// respond to commands. here we are only implementing
// the exit command. in the exit command, cleanup and
// notify that the midlet has been destroyed.
public void commandaction (
command c, displayable s) {
if (c == exitcommand) {
destroyapp(false);
notifydestroyed();
}
}
}
闽公网安备 35060202000074号