Android使用Handler更新ProgressBar

时间:2017-02-25 06:26:25

标签: android handler android-progressbar

我想用Handler和for循环更新progressBar,但没有成功。

代码:

public void increase_splash_bar (int from, int to)
{
    Handler handler1 = new Handler(Looper.getMainLooper()); 
    for (progress_k = from; progress_k<=to ;progress_k++) 
    {
        handler1.postDelayed(new Runnable() 
        {
             @Override
             public void run() 
             {
                  FrontLayout.update_splash_progress_bar(progress_k, 100);
             }
        }, 2000);
    } 
}

问题:

进度条会立即增加到结束值而不是逐步增加。 为什么呢?

3 个答案:

答案 0 :(得分:3)

试试这个:

public void increase_splash_bar (int from, int to)
{
    Handler handler1 = new Handler(Looper.getMainLooper()); 
    for (progress_k = from; progress_k<=to ;progress_k++) 
    {
        final int curr_progress_k = progress_k;
        handler1.postDelayed(new Runnable() 
        {
            @Override
            public void run() 
            {
                 FrontLayout.update_splash_progress_bar(curr_progress_k, 100);
            }
        }, progress_k * 100);      // adjust "100" value to adjust speed
    } 
}

答案 1 :(得分:1)

Repeat a task with a time delay? @inazaruk

private ProgressBar progressBar;
private Handler mHandler;
private int progressInt = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    progressBar = (ProgressBar) findViewById(R.id.pb);
    progressBar.setProgress(0);
    mHandler = new Handler();
    runnable.run();

}


Runnable runnable = new Runnable() {
    @Override
    public void run() {
        try {
            updateProgress();
        } catch (Exception ignored) {

        } finally {
            mHandler.postDelayed(runnable, progressInt);
        }
    }
};

private void updateProgress() {
    progressInt += 1;
    if (progressInt > 100) {
        mHandler.removeCallbacks(runnable);
    } else {
        progressBar.setProgress(progressInt);
    }
}

答案 2 :(得分:0)

试试这段代码:

解决方案1 ​​

public void increase_splash_bar (int from, int to)
{
    Handler handler1 = new Handler(); 
    class Task implements Runnable {

        int start,end;

        Task(int a,int b) { start = a; end = b;}

        @Override
        public void run() {
            for (int i =start ; i <= end; i++) {
                final int value = i;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler1.post(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setProgress(value);
                    }
                });
            }
        }
    }

    Thread t = new Thread(new Task(from, to));   //call it
    t.start();

}

解决方案2:更简单

如果线程太多而无法提出此问题..

您可以使用以下解决方案使用单个Handler来更新进度条:

<强>码

 public class HandlerDemo extends Activity
{
  ProgressBar bar;
  Handler handler = new Handler()
  {
    @Override
    public void handleMessage(Message msg)
    {
      bar.incrementProgressBy(5);
    }
  };
  boolean isRunning = 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; i++)
          {
            Thread.sleep(1000);
            handler.sendMessage(handler.obtainMessage());
          }
        }
        catch (Throwable t)
        {
          // just end the background thread
        }
      }
    });

    isRunning = true;
    background.start();
  }

  public void onStop()
  {
    super.onStop();
    isRunning = false;
  }
}

希望有所帮助......