总路线:
一、TextBox
public class TextBox extends Screen {
//Public Constructors
public TextBox(String title, String text, int maxSize,int constraints);
int constraints:
TextField.ANY,
TextField.EMAILADDR,
TextField.NUMERIC,
TextField.PASSWORD,
TextField.PHONENUMBER,
TextField.URL
ANY
Represents the absence of any constraint, allowing the field to contain any characters
EMAILADDR
Constrains the input to be an e-mail address
NUMERIC
Requires the field to contain a valid number.
PASSWORD
Does not constrain the characters that can be entered, but specifies that they should not
be echoed.
PHONENUMBER
Specifies that the field will contain a phone number. The device may apply checks to
ensure that the value entered conforms with one or more well-known formats for
telephone numbers.
URL
Specifies that the field should contain a Uniform Resource Locator (URL).
// Public Instance Methods
public void delete( int offset, int length);
// 移除从offset 开始的长为length的字符串
public int getCaretPosition();
public int getChars( char[] data);
public int getConstraints();
public int getMaxSize();
public String getString();
public void insert( String src, int position);
public void insert(char[] data, int offset,
int length, int position);
public void setChars(char[] data, int offset,int length);
//替换词从offset开始长为length的字符串
public void setConstraints( int constraints);
//从新设置文本域类型
public int setMaxSize( int maxSize);
public void setString( String text);
//替换整个词为text
public int size();
}
二、Alert
public class Alert extends Screen {
// Public Constructors
public Alert( String title);
public Alert(String title, String alertText,
Image alertImage, AlertType alertType);
// Public Constants
public static final int FOREVER; // =-2
// Property Accessor Methods (by property name)
public int getDefaultTimeout();
public Image getImage();
public void setImage( Image img);
public String getString();
public void setString( String str);
public int getTimeout();
显示一个警告的例子:
Alert a = new Alert("URL", text, null, AlertType.INFO);
display.setCurrent(a);
三、Canvas
Canvas is a Displayable that serves as the most fundamental class of the low-level user
interface API.
public abstract class Canvas extends Displayable {
// Protected Constructors
protected Canvas();
// Public Constants
public static final int DOWN; // =6
public static final int FIRE; // =8
public static final int GAME_A; // =9
public static final int GAME_B; // =10
public static final int GAME_C; // =11
public static final int GAME_D; // =12
public static final int KEY_NUM0; // =48
public static final int KEY_NUM1; // =49
public static final int KEY_NUM2; // =50
public static final int KEY_NUM3; // =51
public static final int KEY_NUM4; // =52
public static final int KEY_NUM5; // =53
public static final int KEY_NUM6; // =54
public static final int KEY_NUM7; // =55
public static final int KEY_NUM8; // =56
public static final int KEY_NUM9; // =57
public static final int KEY_POUND; // =35
public static final int KEY_STAR; // =42
public static final int LEFT; // =2
public static final int RIGHT; // =5
public static final int UP; // =1
// Property Accessor Methods (by property name)
public boolean isDoubleBuffered();
public int getHeight();
public int getWidth();
// Public Instance Methods
public int getGameAction( int keyCode);
public int getKeyCode( int gameAction);
public String getKeyName( int keyCode);
public boolean hasPointerEvents();
public boolean hasPointerMotionEvents();
public boolean hasRepeatEvents();
public final void repaint();
public final void repaint(int x, int y, int width,int height);
//重新绘制显示区域
public final void serviceRepaints();
// Protected Instance Methods
protected void hideNotify(); // empty
protected void keyPressed( int keyCode); // empty
protected void keyReleased( int keyCode); // empty
protected void keyRepeated( int keyCode); // empty
protected abstract void paint( Graphics g);
//绘制显示区域
protected void pointerDragged( int x, int y); // empty
protected void pointerPressed( int x, int y); // empty
protected void pointerReleased( int x, int y); // empty
protected void showNotify(); // empty
}
四、Form
public class Form extends Screen {
// Public Constructors
public Form( String title);
public Form( String title, Item[] items);
// Public Instance Methods
public int append( String str);
public int append( Image image);
public int append( Item item);
//Item类如下定义:---------------------------------------------------------------------------------
public abstract class Item {
// No Constructor
// Public Instance Methods
public String getLabel();
public void setLabel( String label);
}
Subclasses
ChoiceGroup, DateField, Gauge, ImageItem, StringItem, TextField
//-----------------------------------------------------------------------------------------------------------
public void delete( int index);
public Item get( int index);
public void insert( int index, Item item);
public void set( int index, Item item);
public void setItemStateListener(
ItemStateListener iListener);
public int size();
}
五、List
构造:
private List implicitList;
implicitList = new List("Implicit", Choice.IMPLICIT, stringArray,
imageArray);
implicitList.addCommand(CMD_BACK);
implicitList.addCommand(CMD_EXIT);
implicitList.setCommandListener(this);
显示:
display.setCurrent(implicitList);
六、DateField
public class DateField extends Item {
// Public Constructors,他是item的子类
public DateField( String label, int mode);
// mode可以选择的有:DATE, TIME, or DATE_TIME,
public DateField(String label, int mode,java.util.TimeZone timeZone);
// Public Constants
public static final int DATE; // =1
public static final int DATE_TIME; // =3
public static final int TIME; // =2
// Public Instance Methods
public java.util.Date getDate();
public int getInputMode();
public void setDate( java.util.Date date);
public void setInputMode( int mode);
// Public Methods Overriding Item
public void setLabel( String label);
}
七、Gauge
public class Gauge extends Item {
// Public Constructors
public Gauge(String label, boolean interactive,
int maxValue, int initialValue);
// Public Instance Methods
public int getMaxValue();
public int getValue();
public boolean isInteractive();
public void setMaxValue( int maxValue);
public void setValue( int value);
// Public Methods Overriding Item
public void setLabel( String label);
八、ImageItem
public class ImageItem extends Item {
// Public Constructors
public ImageItem(String label, Image img, int layout,
String altText);
// Public Constants
public static final int LAYOUT_CENTER; // =3
public static final int LAYOUT_DEFAULT; // =0
public static final int LAYOUT_LEFT; // =1
public static final int LAYOUT_NEWLINE_AFTER; // =512
public static final int LAYOUT_NEWLINE_BEFORE; // =256
public static final int LAYOUT_RIGHT; // =2
// Public Instance Methods
public String getAltText();
public Image getImage();
public int getLayout();
public void setAltText( String altText);
public void setImage( Image img);
public void setLayout( int layout);
// Public Methods Overriding Item
public void setLabel( String label);
}
九、ChoiceGroup
public class ChoiceGroup extends Item implements Choice {
// Public Constructors
public ChoiceGroup( String label, int choiceType);
//可选择的模式有:
A Choice can operate in one of three different modes:
EXCLUSIVE
Only one alternative can be selected. If the user selects one item from the set of
offered alternatives, any item already selected is deselected. In this mode, the control
behaves as (and is usually rendered to look like) a set of radio buttons.
MULTIPLE
Any number of alternatives can be selected. Selecting one item from the list has no
effect on the selected state of other entries. In this, the control behaves like a collection
of check boxes.
IMPLICIT
This mode is available only with the List control. It allows only one item to be
selected at any given time and is typically used to create a menu. The difference
between this mode and EXCLUSIVE, apart from the visual differences, lies in the way in
which selection changes are notified to application code. See the description of the
List control for details.
public ChoiceGroup(String label, int choiceType,String[] stringElements,Image[] imageElements);
// Methods Implementing Choice
public int append(String stringElement,Image imageElement);
public void delete( int index);
public Image getImage( int i);
public int getSelectedFlags(boolean[] selectedArray_return);
public int getSelectedIndex();
public String getString( int i);
public void insert(int index, String stringElement,Image imageElement);
public boolean isSelected( int index);
public void set(int index, String stringElement,Image imageElement);
public void setSelectedFlags(boolean[] selectedArray);
//the method sets the state of every element in the Choicefrom the corresponding entry //in an arrayof boolean values supplied as its argument. InEXCLUSIVE and IMPLICIT //modes, only one entry in the array can have value true.
public void setSelectedIndex(int index, boolean selected);
public int size();
// Public Methods Overriding Item
public void setLabel( String label);
十、Command&& CommandListener
Command是一个构造用户操作接口命令的类。是一个命令消息的发出装置。
对于Form类的Command会以可视化在手机屏幕的左或右下脚显示.
可以在类的初始化时期构造这种接口:addCommand(new Command("Back", Command.BACK, 1));
public class Command {
// Public Constructors
public Command(String label, int commandType,int priority);
// Public Constants
public static final int BACK; // =2
public static final int CANCEL; // =3
public static final int EXIT; // =7
public static final int HELP; // =5
public static final int ITEM; // =8
public static final int OK; // =4
public static final int SCREEN; // =1
public static final int STOP; // =6
// Public Instance Methods
public int getCommandType();
public String getLabel();
public int getPriority();
}
l 声明一个命令:
1)Command cmd1=new Command("查看详情", Command.EXIT, 1);
//除了ITEM外的都属于一般性命令。
2)Command cmd2=new Command("查看详情", Command.ITEM, 2);
//可以附载于ITEM的命令
l 为类增加命令:
1) 继承于Displayable的类(Canvas,FORM,ALERT,TEXTBOX,LIST)
displayable.addCommand(cmd1);
2) 继承与CustemItem的类
item.addCommand(cmd1);
l 增加监听器:
1)displayable.setCommandListener(this);
//this类必须派生于implements CommandListener
2)Item.setItemCommandListener(this);
//this类必须派生于implements ItemCommandListener
l 设置监听器函数:
1)public void commandAction(Command command, Displayable displayable) {
if (command.getCommandType() == Command.EXIT) {
// 以类型来辨别消息
}
}
2)public void commandAction(Command command,Item item){
if(command=cmd2)//以命令的实体来区别消息
{//…}
}
十一 ItemStateListener
介绍: An interface implemented by objects that want to be notified when the state of an Item ischanged as the result of user action. Each Form can have a single ItemStateListener that is registered by calling its setItemStateListener() method. Programmatic changes to the state of an Item are never notified to the ItemStateListener.
该接口只能处理程序中Form内的交互项目。
public interface ItemStateListener {
// Public Instance Methods
public abstract void itemStateChanged( Item item);//通过item实体来辨别是哪个发生了状态改变。
}
十二、如何生成BUTTON
1) StringItem Item1 = new StringItem("login", "",Item.BUTTON);
对该按钮可以设置事件:
Item1.addCommand(c);
2) 继承CustomItem
public class Button extends CustomItem{
private static final int HEIGHT = 20;
private static final int WIDTH = 20;
private static final int DELTA = 2;
private String num;
private KeyPad pad;
public Button(KeyPad pad, String num){
super("");
this.pad = pad;
this.num = num;
}
protected void paint(Graphics g, int w, int h){
g.setColor(0, 0, 0);
g.fillRect(0, 0, WIDTH+DELTA, HEIGHT + DELTA);
g.setColor(128, 128, 128);
g.fillRect(0, 0, WIDTH-DELTA, HEIGHT-DELTA);
int xOffset = WIDTH/3;
int yOffset = 2*HEIGHT/3;
g.setColor(255, 255, 255);
g.drawString(num, xOffset, yOffset, Graphics.BASELINE | Graphics.LEFT);
}
protected int getPrefContentHeight(int width) {
return getMinContentWidth();
}
protected int getPrefContentWidth(int height) {
return getMinContentHeight();
}
protected int getMinContentHeight() {
return HEIGHT;
}
protected int getMinContentWidth() {
return WIDTH;
}
// Called by the system when a pointer down action (for example, UIQ系列的笔触动作) has occurred within the item.
protected void pointerPressed(int x, int y) {
pad.setString(num);
}
//普通的按下动作处理
public void keyPressed(int keyCode){
int gameAction = getGameAction(keyCode);
if (gameAction == Canvas.FIRE){
pad.setString(num);
}
}
}