0
以下代碼來自Beginning Android 3,Chapter 20.當手機旋轉時,將創建一個新的活動並調用onStart(),所以bar.setProgress() 0)被調用。但是,我沒有看到酒吧的進展回到了開始。爲什麼不?bar.setProgress(0)不起作用
public class HandlerDemo extends Activity {
ProgressBar bar;
Handler handler=new Handler() {
@Override
public void handleMessage(Message msg) {
bar.incrementProgressBy(5);
}
};
AtomicBoolean isRunning=new AtomicBoolean(false);
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
bar=(ProgressBar)findViewById(R.id.progress);
}
public void onStart() {
super.onStart();
bar.setProgress(0);
Thread background=new Thread(new Runnable() {
public void run() {
try {
for (int i=0;i<20 && isRunning.get();i++) {
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
} catch (Throwable t) {
// just end the background thread
}
}
});
isRunning.set(true);
background.start();
}
public void onStop() {
super.onStop();
isRunning.set(false);
}
}
閱讀:) http://stackoverflow.com/questions/4348032/android-progressbar-does-not-update-progress-view-drawable – lv0gun9 2014-12-03 05:46:39