[转贴NEC]有效利用UI部件(3)
有效利用UI部件(3)
![]() | 在过去的两期讲座里我们对UI部件的使用方法作了说明,内容大体上都明白了吧? |
![]() | 是的。现在知道了根据派生类(Item 类或Screen类)的不同,其使用方法也是不同的。但是,还是有很多UI部件的使用方法还没有讲到呢。 |
![]() | 这一次,我们就来介绍ChoiceGroup、ImageItem和CustomItem的使用方法。之后还会有练习题哦!这样一来,虽然所有的UI部件不能一一介绍,但主要部分都能得以详细得说明。关于UI部件的解说,暂且就在此讲告一段落了。 |
![](http://www.nec-mfriend.com/cn/lecture/images/c3_1.jpg)
1. ChoiceGroup
![]() | 那么,我们首先来说明一下ChoiceGroup类。ChoiceGroup从多个选项里选择一个用户,再选择多个Item后就能使用了。 |
![]() | 咦?这跟上次讲的List类很相似啊。 |
![]() | 是这样的。不同的是,List类派生于Screen类,而ChoiceGroup是从Item类派生而来的。并且能在Form Object中,搭载多个ChoiceGroup。 |
![]() | 也同List类一样,能有多种不同的使用方法吗? |
![]() | 是的。既能选择一项,也可以选择多项。用下列代码来运行的程序中,就使用了3种不同类型的ChoiceGroup。 |
ChoiceGroupTest.java
1
| import javax.microedition.midlet.MIDlet; | ||
2
| import javax.microedition.lcdui.*; | ||
3
| |||
4
| public class ChoiceGroupTest extends MIDlet implements CommandListener { | ||
5
| |||
6
| // 制定指令 | ||
7 | private Command exitCommand = new Command("Exit", Command.EXIT, 1); | ||
8 | private Command okCommand = new Command("OK", Command.SCREEN, 1); | ||
9 | |||
10 | // 生成EXCLUSIVE类型的ChoiceGroup Object | ||
11 | ChoiceGroup cgExclusive = new ChoiceGroup("Exclusive", ChoiceGroup.EXCLUSIVE); | ||
12 | |||
13 | // 生成MULTIPLE类型的ChoiceGroup Object | ||
14 | ChoiceGroup cgMultiple = new ChoiceGroup("Multiple", ChoiceGroup.MULTIPLE); | ||
15 | |||
16 | // 生成POPUP类型的ChoiceGroup Object | ||
17 | ChoiceGroup cgPopup = new ChoiceGroup("Popup", ChoiceGroup.POPUP); | ||
18 | |||
19 | private boolean firstTime; | ||
20 | private Form mainForm; | ||
21 | private Display display; | ||
22 | |||
23 | public ChoiceGroupTest() { | ||
24 | firstTime = true; | ||
25 | } | ||
26 | |||
27 | protected void startApp() { | ||
28 | if (firstTime) { | ||
29 | mainForm = new Form("Choice Group Test"); | ||
30 | |||
31 | // 对EXCLUSIVE类型的ChoiceGroup Object进行Item注册 | ||
32 | cgExclusive.append("Small", null); | ||
33 | cgExclusive.append("Medium", null); | ||
34 | cgExclusive.append("Large", null); | ||
35 | |||
36 | // 对MULTIPLE类型的ChoiceGroup Object进行Item注册 | ||
37 | cgExclusive.append("Red", null); | ||
38 | cgExclusive.append("Blue", null); | ||
39 | cgExclusive.append("Green", null); | ||
40 | |||
41 | // 对POPUP类型的ChoiceGroup Object进行附着图标的Item注册 | ||
42 | try { | ||
43 | cgPopup.append("Nikki", Image.createImage("/nikki.png")); | ||
44 | cgPopup.append("Professor", Image.createImage("/professor.png")); | ||
45 | } catch(Exception e) { System.out.println(e); } | ||
46 | |||
47 | // 追加ChoiceGroup Object | ||
48 | mainForm.append(cgExclusive); | ||
49 | mainForm.append(cgMultiple); | ||
50 | mainForm.append(cgPopup);; | ||
51 | |||
52 | // 追加指令 | ||
53 | mainForm.addCommand(exitCommand); | ||
54 | mainForm.addCommand(okCommand); | ||
55 | |||
56 | // 注册CommandListener | ||
57 | mainForm.setCommandListener(this); | ||
58 | firstTime = false; | ||
59 | } | ||
60 | display = Display.getDisplay(this); | ||
61 | display.setCurrent(mainForm); | ||
62 | } | ||
63 | |||
64 | public void commandAction(Command c, Displayable s) { | ||
65 | if (c == exitCommand) { | ||
66 | // 选择“Exit”时的处理 | ||
67 | // 程序结束 | ||
68 | destroyApp(false); | ||
69 | notifyDestroyed(); | ||
70 | } else if(c == okCommand) { | ||
71 | // 选择“OK”时的处理 | ||
72 | |||
73 | // 显示被选中的Item | ||
74 | String message = | ||
75 | getSelectedItemString(cgExclusive) + "/n" | ||
76 | + getSelectedItemString(cgMultiple) + "/n" | ||
77 | + getSelectedItemString(cgPopup) + "/n"; | ||
78 | |||
79 | Alert a = new Alert("Selected Items", message, null, AlertType.INFO); | ||
80 | display.setCurrent(a); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | // 制作表示被ChoiceGroup Object选中的Item的字符串 | ||
85 | private String getSelectedItemString(ChoiceGroup cg) { | ||
86 | String itemString = ""; | ||
87 | boolean flags[] = new boolean[3]; | ||
88 | |||
89 | // 取得表示各Item选择状态的boolean型排列 | ||
90 | cg.getSelectedFlags(flags); | ||
91 | |||
92 | for(int i = 0; i < cg.size(); i++) { | ||
93 | if(flags[i]) { // 若标志为true,表示被选中 | ||
94 | itemString += "[" + cg.getString(i) + "]"; | ||
95 | } | ||
96 | } | ||
97 | return itemString; | ||
98 | } | ||
99 | |||
100 | protected void destroyApp(boolean unconditional) { | ||
101 | } | ||
102 | |||
103 | protected void pauseApp() { | ||
104 | } | ||
105 | } | ||
![]() | 首先来试着实际运行一下上面的代码吧。 |
![]() | 好的。 |
![](http://www.nec-mfriend.com/cn/lecture/images/MIDP2.0_14_1.jpg)
![](http://www.nec-mfriend.com/cn/lecture/images/MIDP2.0_14_2.jpg)
![](http://www.nec-mfriend.com/cn/lecture/images/MIDP2.0_14_3.jpg)
![]() | ChoiceGroup类的Constructor变为下面的形式,可以通过第二位参数来指定类型。 |
ChoiceGroup(String label, int choiceType) |
![]() | 在上面的代码中,按顺序分别使用了“ChoiceGroup.EXCLUSIVE”“ChoiceGroup.MULTIPLE”“ChoiceGroup.POPUP”,是吧。 |
![]() | 是的。通过指定值的不同,来确认运行不同的处理。 |
![]() | 在POPUP类型的ChoiceGroup中,显示了我跟博士的图标呢。也能做这种程度的设定吗? |
![]() | 给ChoiceGroup追加选项,是通过下面的指令来完成的。在这里可以一起指定字符串和图标。第二位参数指定为表示图标图像的Image Object,若不需要图标时则指定为null。 |
append(String stringPart, Image imagePart) |
![]() | 因为有时用户会选择多个Item,所以我们运用下面的指令,通过boolean型的排列来区分选项是否被选中。 |
getSelectedFlags(boolean[] selectedArray_return) |
![]() | 实际使用方法请参照代码的第90~96行。 |
2.有效利用ImageItem
![]() | 接着我们来介绍ImageItem。ImageItem类,顾名思义就是“图像”的部件,跟上一讲介绍的StringItem非常相似。 |
![]() | 只是将String变为了 Image吗? |
![]() | 也可以这么说。功能几乎跟StringItem一样,只是Constructor如下表所示,参数的数目增加了。 |
public ImageItem(String label, |
![]() | 第2位的参数用于指定显示的image. 第3位的参数用于指定被Item定义的布局方法,它的数值很多,如下所示: LAYOUT_DEFAULT LAYOUT_LEFT LAYOUT_RIGHT LAYOUT_CENTER LAYOUT_TOP LAYOUT_BOTTOM LAYOUT_VCENTER LAYOUT_NEWLINE_BEFORE LAYOUT_NEWLINE_AFTER LAYOUT_SHRINK LAYOUT_VSHRINK LAYOUT_EXPAND LAYOUT_VEXPAND LAYOUT_2 可以从中选择出想要的数值随意组合使用,详细方法请参照API使用方法说明。 第5位的参数可以指定image的显示方法,其数值种类的说明如下表所示。 |
参数值
|
说明
|
Item.PLAIN | 生成普通字符串(与没有指定第3位参数时的作用相同) |
Item.HYPERLINK | 生成超级链接形式的字符串 |
Item.BUTTON | 生成按键 |
![]() | 下面的代码就是使用了不同种类ImageItem的例子。 |
ImageItemTest.java
1
| import javax.microedition.midlet.MIDlet; | ||
2
| import javax.microedition.lcdui.*; | ||
3
| |||
4
| public class ImageItemTest extends MIDlet | ||
5
| implements CommandListener, ItemCommandListener { | ||
6
| |||
7 | // 指定指令 | ||
8 | private Command exitCommand = new Command("Exit", Command.EXIT, 1); | ||
9 | private Command hyperLinkCommand = new Command("Link", Command.ITEM, 1); | ||
10 | private Command buttonCommand = new Command("Button", Command.ITEM, 1); | ||
11 | |||
12 | private ImageItem plainImage; | ||
13 | private ImageItem hyperlinkImage; | ||
14 | private ImageItem buttonImage; | ||
15 | |||
16 | private boolean firstTime; | ||
17 | private Form mainForm; | ||
18 | private Display display; | ||
19 | |||
20 | public ImageItemTest() { | ||
21 | firstTime = true; | ||
22 | mainForm = new Form("ImageItemTest"); | ||
23 | } | ||
24 | |||
25 | protected void startApp() { | ||
26 | if (firstTime) { | ||
27 | // 读取image | ||
28 | Image image = null; | ||
29 | try { | ||
30 | image = Image.createImage("/nikki.png"); | ||
31 | } catch (Exception e) {} | ||
32 | |||
33 | // 生成PLAIN型的ImageItem | ||
34 | plainImage = new ImageItem("", image, Item.LAYOUT_VCENTER, "", Item.PLAIN); | ||
35 | |||
36 | // 生成HYPERLINK型的ImageItem | ||
37 | hyperlinkImage = new ImageItem("", image, Item.LAYOUT_VCENTER, | ||
38 | "", Item.HYPERLINK); | ||
39 | |||
40 | // 生成BUTTON型的ImageItem | ||
41 | buttonImage = new ImageItem("", image, Item.LAYOUT_VCENTER, | ||
42 | "", Item.BUTTON); | ||
43 | |||
44 | // 追加ImageItem | ||
45 | mainForm.append(plainImage); | ||
46 | mainForm.append(hyperlinkImage); | ||
47 | mainForm.append(buttonImage); | ||
48 | |||
49 | // 追加指令 | ||
50 | mainForm.addCommand(exitCommand); | ||
51 | |||
52 | // 对HYPERLINK和BUTTON型的ImageItem进行指令设定 | ||
53 | hyperlinkImage.setDefaultCommand(hyperLinkCommand); | ||
54 | buttonImage.setDefaultCommand(buttonCommand); | ||
55 | |||
56 | // 注册CommandListener | ||
57 | mainForm.setCommandListener(this); | ||
58 | hyperlinkImage.setItemCommandListener(this); | ||
59 | buttonImage.setItemCommandListener(this); | ||
60 | firstTime = false; | ||
61 | } | ||
62 | display = Display.getDisplay(this); | ||
63 | display.setCurrent(mainForm); | ||
64 | } | ||
65 | |||
66 | public void commandAction(Command c, Item i) { | ||
67 | if(c == hyperLinkCommand) { | ||
68 | //选择HYPERLINK型ImageItem时的处理 | ||
69 | Alert a = new Alert("Link", "HyperLink was selected.", | ||
70 | null, AlertType.INFO); | ||
71 | display.setCurrent(a); | ||
72 | } else if(c == buttonCommand) { | ||
73 | //选择BUTTON型ImageItem时的处理 | ||
74 | Alert a = new Alert("Button", "Button was pushed.", | ||
75 | null, AlertType.INFO); | ||
76 | display.setCurrent(a); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | public void commandAction(Command c,Displayable s) { | ||
81 | if(c == exitCommand) { | ||
82 | // 选择“Exit”时的处理 | ||
83 | // 程序结束 | ||
84 | destroyApp(false); | ||
85 | notifyDestroyed(); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | public void destroyApp(boolean unconditional) { | ||
90 | } | ||
91 | |||
92 | public void pauseApp() { | ||
93 | } | ||
94 | } | ||
![]() | 实际运行以上代码后,三种方法所生成的image 就如下图所示。左起按顺序分别为Item.PLAIN、Item.HYPERLINK、Item.BUTTON所指定图片。选择方法与上一讲使用StringItem时相同。 |
![](http://www.nec-mfriend.com/cn/lecture/images/MIDP2.0_14_4.jpg)
![](http://www.nec-mfriend.com/cn/lecture/images/MIDP2.0_14_5.jpg)