1. 程式人生 > >ProgressBar進度條10秒顯示完成進度工作。

ProgressBar進度條10秒顯示完成進度工作。

之前我一個朋友面試遇到了一個問題。頁面上現有ProgressBar控制元件progressBar,請用書寫執行緒以10秒的的時間完成其進度顯示工作。  

所以在這兒分享下希望幫助到一些朋友!

下面是程式碼:

public class ProgressBarStu extends Activity {
  private ProgressBar progressBar = null;

  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.progressbar);
     progressBar = (ProgressBar) findViewById(R.id.progressBar);
   Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
     int progressBarMax = progressBar.getMax();
     try {
      while (progressBarMax != progressBar.getProgress()) {
       int stepProgress = progressBarMax / 10;
       int currentprogress = progressBar.getProgress();
       progressBar.setProgress(currentprogress
         + stepProgress);
       Thread.sleep(1000);
      }
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   });
   thread.start();
  }
 }

 大神繞道!本人只為分享共贏!