5

我在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(); 

但它也不起作用。

第二段代码确实有效,问题出在我的逻辑上。

4

4 回答 4

6

你可以试试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();
于 2013-06-04T09:57:33.620 回答
3

这将更新状态栏,直到达到最大值:

private int progress = 0;
private final int pBarMax = 60;

...

final ProgressBar pBar = (ProgressBar) findViewById(R.id.progressBar1);     
pBar.setMax(pBarMax);
final Thread pBarThread = new Thread() {
    @Override
    public void run() {
        try {
            while(progress<=pBarMax) {
                pBar.setProgress(progress);
                sleep(1000);
                ++progress; 
            }
        }
        catch(InterruptedException e) {
        }
    }
};

pBarThread.start();
于 2013-06-04T10:34:13.200 回答
2

您可以为此尝试此代码:

 Thread timer = new Thread(){
public void run(){
    try{
        sleep(10000);
        while(progressBarStatus < 10000){
            StartPoint.this.runOnUIThread(new Runnable(){
                public void run()
                {
                    progressBar.setProgress(progressBarStatus);
                    progressBarStatus += 1000;
                }
            });

        }
    }catch(InterruptedException e){
        e.printStackTrace();
    }finally{

    }
}
};
timer.start();
于 2013-06-04T09:47:29.117 回答
2

这段代码对我有用

new Thread(new Runnable() {

                public void run() {
                    long timerEnd = System.currentTimeMillis() + 60 * 1000;

                    while (timerEnd >  System.currentTimeMillis()) {

                        progress = 60 - (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("App","Progress thread cannot sleep");
                        }
                    }
                    progressHandler.post(new Runnable() {
                        public void run() {                     
                                okButton.performClick();                    
                        }                   
                    });
                }
            }).start(); 
于 2013-06-04T10:28:20.590 回答