Android ProgressBar的使用



Android 基础教程之-------Android ProgressBar的使用


FAQ:

ProgressBar 样式? 样式自定义?



// ProgressBar
protected static final int STOP = 0x10000;
protected static final int NEXT = 0x10001;

private int iCount = 0;

rectangleProgressBar.setVisibility(View.VISIBLE);
circleProgressBar.setVisibility(View.VISIBLE);

rectangleProgressBar.setMax(100);
rectangleProgressBar.setProgress(0);
circleProgressBar.setProgress(0);

//创建一个线程,每秒步长为5增加,到100%时停止
Thread mThread = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 20; i++) {
try {

iCount = (i + 1) * 5;
Thread.sleep(1000);

if (i == 19) {
Message msg = new Message();
msg.what = STOP;
mHandler.sendMessage(msg);
break;
} else {
Message msg = new Message();
msg.what = NEXT;
mHandler.sendMessage(msg);
}

} catch (Exception e) {
e.printStackTrace
}
}
}
});

//定义一个Handler
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case STOP:
rectangleProgressBar.setVisibility(View.GONE);
circleProgressBar.setVisibility(View.GONE);
Thread.currentThread().interrupt();
break;

case NEXT:
if (!Thread.currentThread().isInterrupted()) {
rectangleProgressBar.setProgress(iCount);
circleProgressBar.setProgress(iCount);
}
}
}
}