---
title: "黑莓学习笔记之一----进度条"
author: "flyouting"
date: "2011-05-25T00:24:05Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2011-05-25T00:24:05Z"
id: 1057314
url: "https://flyouting.iteye.com/blog/1057314"
---

最近突然想把自己公司的项目移植到黑莓上来，因为听说黑莓的app挺少的，而且黑莓开发貌似比较繁琐，不如android和iPhone的便捷，反正闲着没事，就买了本书，找了点资料开始学习学习。学一点记录一点吧。

    一般项目里最一开始都会有一个进度条加载，然后是主界面或者是登录界面，那就先看看这个进度条是怎么做的。
    
    
```java
package mypackage;

import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class GaugeScreen extends PopupScreen {

	public GaugeScreen(){
		super(new VerticalFieldManager(), VerticalFieldManager.VERTICAL_SCROLL);
		add(new LabelField("Loading"));
		
		//public GaugeField(String label,int min,int max,int start,long style);
		GaugeField percentGauge = new GaugeField("",0,100,0,GaugeField.PERCENT);
		
		add(percentGauge);
	}
	// TODO Auto-generated constructor stub}

}

```

    
    自定义一个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). 

    
```java
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，多数也是用到了这个方法来延迟更新。
    
    剩下的就是添加一个菜单项，来处理这个，当点击了菜单项时，演示这个进度条。
 
    
```java
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);
	}
```



![](http://dl.iteye.com/upload/attachment/487209/5cf885c4-a461-3b81-9171-11bf06128640.jpg)