天天看點

黑莓學習筆記之三--對話框和菜單欄

菜單項:我們通過在Screen中重寫makeMenu()方法來添加菜單。

protected void makeMenu(Menu menu,int instance)

在這個方法裡,會用到menu.add(MenuItem)來添加一個菜單項。

菜單項這個類就是 MenuItem

MenuItem newItem = new MenuItem("Alert", 100, 10){
			public void run(){
				Dialog.inform("Alert Message");
			}
		};
           

MenuItem構造子接受下面的3個參數:

text 菜單項的名稱

ordinal 菜單項的順序;一個越大的值表明了這個菜單項越靠近菜單的底部。

priority 接收預設焦點的菜單項優先級(值越小表示高優先級)

run()定義了當使用者點選菜單項發生的操作的實作。

對話框: Dialog 繼承 PopupScreen 并實作 FieldChangeListener, HolsterListener

1、Dialog由标題區和使用者區組成。使用者區包含圖示和标題資訊;使用者區主要由按鍵組成。

2、Dialog的排版由DialogManager管理,客戶區實際按Vertical居中排列按鍵,當然也可添加其他Field

3、Dialog是局部的類,它預設從屬于類的某個方法,當Style設定了Dialog. GLOBAL_STATUS後,它就成為全局類,可以在類的任何地方顯示執行,包括在構造方法内。

4、Dialog的Styel除了Screen和Manager外,還有LIST和GLOBAL_STATUS,LIST表示使用者區的按鍵顯示形式不是原來的Button形式,而是清單的形式。GLOBAL_STATUS見前。

5、Dialog有預制、按标準定制和非标準定制三種。

按标準定制

系統設定好幾個按鍵的組合形式,包括有

D_OK, 顯示一個字元串,并且提示使用者點選OK。

D_SAVE, 實作一個字元串,并且提示使用者點選Save,Discard,或者Cancel;按Escape取消

D_DELETE, 顯示一個字元串,并且提示使用者點選Delete或者Cancel;按Escape撤銷

D_YES_NO. 顯示一個字元串,并且提示使用者點選Yes或No

這些組合根據按鍵按下對應傳回CANCEL、OK、SAVE、DISCARD、DELETE、YES和NO等值。标準定制時每次type隻能選擇一個組合。

Ask除Bitmap已确定為“?”外,其他同Dialog構造函數。

defaultChoice表示一顯示,直接聚焦到第幾個按鍵。

dontAskAgain – 增加一個’Don’t ask again’ 的檢查框。

Style=0和1(List)兩種,其他值未發現有效,即Screen和Manager的style不能影響dialog的樣式。

非标準定制

choices表示要添加到使用者區作按鍵的object,Dialog隻是提取他們的string(Object.String)作按鍵的Label而已,是以對Object添加string就可以了。

values是對應object的傳回值,如果沒有,傳回值用序号(index)代替。

public static int ask(String message,  Object[] choices, int defaultChoice)

public static int ask(String message, Object[] choices,  int[] values,

int defaultChoice)



 public Dialog(String message, Object[] choices,int[] values, int defaultChoice, Bitmap bitmap)

public Dialog(String message, Object[] choices, int[] values, int defaultChoice, Bitmap bitmap, long style)
           

一般方法

public int doModal()//執行Dialog,并傳回按下的鍵值。

public void show()//顯示Dialog,無傳回值。
public void show(int priority)

protected static String[] getResourceChoices(int type)

protected static String getResourceMessage(int type)

protected static int[] getResourceValues(int type)

protected static int getResourceDefaultValue(int type)

protected static int[] getResourceSoftkeyMap(int type)

public void setDialogClosedListener(DialogClosedListener listener)

public RichTextField getLabel()

public int getPreferredWidth()

public int getSelectedValue()

public boolean getDontAskAgainValue()

public final void setDefault(int defaultChoice)

public final void setEscapeEnabled(boolean escapeEnabled)
           

Dialog 選擇項處理方式,如下:

int response = Dialog.ask(Dialog.D_SAVE);

if (Dialog.SAVE == response || Dialog.CANCEL == response)


return
false;

if ( Dialog.DISCARD == response )


_item.deleteItem(_itemIndex);
           

幾個聲明的範例:

[img]http://dl.iteye.com/upload/attachment/488552/02df916d-703f-344a-a335-14a72a49e6fb.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/488550/59836e99-31e5-390a-a917-238fbb22b5c6.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/488548/ab763594-d73c-3582-82f9-eac3743e10e1.jpg[/img]

繼續閱讀