我在 android 中有一个水平进度条。我需要在 60 秒内完成。
为什么下面的代码不起作用?
int progress = 0;
progressBarHorizontal.setMax(60);
while(progress < 60) {
progressHandler.postDelayed((new Runnable() {
public void run() {
progressBarHorizontal.setProgress(progress);
}
}),progress * 1000);
progress++;
}
请推荐一些其他的方法。我试过这个:
new Thread(new Runnable() {
int progress = 0;
public void run() {
long timerEnd = System.currentTimeMillis() + 60 * 1000;
while (timerEnd > System.currentTimeMillis() ) {
progress = (int) (timerEnd - System.currentTimeMillis()) / 1000;
// Update the progress bar
progressHandler.post(new Runnable() {
public void run() {
progressBarHorizontal.setProgress(progress);
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Log.w("tag","Progress thread cannot sleep");
}
}
}
}).start();
但它也不起作用。
第二段代码确实有效,问题出在我的逻辑上。
最佳答案
你可以试试 CountDownTimer
:
pd = ProgressDialog.show(MovementEntry.this, "", "Please Wait...",
true, false);
pd.show();
new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
//this will be done every 1000 milliseconds ( 1 seconds )
int progress = (60000 - millisUntilFinished) / 1000;
pd.setProgress(progress);
}
@Override
public void onFinish() {
//the progressBar will be invisible after 60 000 miliseconds ( 1 minute)
pd.dismiss();
}
}.start();
关于android - 如何显示1分钟的进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16914566/