最近突然想把自己公司的項目移植到黑莓上來,因為聽說黑莓的app挺少的,而且黑莓開發貌似比較繁瑣,不如android和iPhone的便捷,反正閑着沒事,就買了本書,找了點資料開始學習學習。學一點記錄一點吧。
一般項目裡最一開始都會有一個進度條加載,然後是主界面或者是登入界面,那就先看看這個進度條是怎麼做的。
自定義一個popupScreen,其中加入一個文字辨別:“Loading”,加入一個進度條控件,進度條控件的構造方法是public GaugeField(String label,int min,int max,int start,long style); 其中參數的含義是
label - Optional label for the gauge (may be null).//辨別的字元串
min - Bottom of the value range. //最小值
max - Top of the value range. //最大值
start - Initial progress level of this field. //起始值
style - Style value(s) for this field. //進度條的風格
風格包含有(Field.FOCUSABLE, Field.EDITABLE, GaugeField.NO_TEXT, GaugeField.PERCENT).
package mypackage;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.container.PopupScreen;
public class UpdaterThread extends Thread {
PopupScreen screen;
int i;
public UpdaterThread(PopupScreen screen){
this.screen = screen;
}
public void run(){
for(i = 0;i<100;i++){
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
GaugeField Gauge = (GaugeField) screen.getField(1);
Gauge.setValue(i);
}
});
}
}
}
自定義一個線程去控制進度條的數值大小,我們需要把剛才的Screen穿進去,然後有一個方法public final void invokeLater(Runnable runnable),這個方法可以把Runnable對象到這個應用程式的事件隊列中。在所有排程事件處理後,會處理此Runable對象的擁有者。其實就是相當于一個線程隊列。
這個被成為延遲調用的方法,多數用于既執行了自己的操作,又不影響原本程式的運作。比如調起一個錯誤資訊的提示對話框。在背景線程中更新前台的UI,多數也是用到了這個方法來延遲更新。
剩下的就是添加一個菜單項,來處理這個,當點選了菜單項時,示範這個進度條。
protected void makeMenu(Menu arg0, int arg1) {
// TODO Auto-generated method stub
MenuItem newItem = new MenuItem("Build", 100, 10){
public void run(){
GaugeScreen screen = new GaugeScreen();
UiApplication.getUiApplication().pushScreen(screen);
UpdaterThread thread = new UpdaterThread(screen);
thread.start();
}
};
arg0.add(newItem);
super.makeMenu(arg0, arg1);
}
[img]http://dl.iteye.com/upload/attachment/487209/5cf885c4-a461-3b81-9171-11bf06128640.jpg[/img]