【安卓笔记】Handler:10秒钟时间显示进度条

 

方式一:使用Handler

 

 

 public class ProgressActivity extends Activity implements Runnable {
	private ProgressBar progress;
	Handler h = new Handler();
	private int max=100,current,step;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_progress);
		progress = (ProgressBar) findViewById(R.id.progress);
		progress.setMax(max);
		progress.setProgress(0);
		step = max/10;
		h.post(this);
	
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void run() {
		current = progress.getProgress();
		progress.setProgress(current+step);
		h.postDelayed(this, 1000);
	}

}


【安卓笔记】Handler:10秒钟时间显示进度条

 方式二:使用自定义线程

               有些微不对劲

public class ProgressActivity extends Activity {
	private ProgressBar progress;
	private int max = 100, current = 0, step = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_progress);
		progress = (ProgressBar) findViewById(R.id.progress);
		progress.setMax(max);
		progress.setProgress(0);
		step = max / 10;

		new Thread(new Runnable() {
			int i = 1;

			@Override
			public void run() {

				try {
					while (max != progress.getProgress()) {
						Log.i("次数", i + "");
						i++;
						progress.setProgress(current + step);
						current = progress.getProgress();
						Thread.sleep(1000);
					}

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

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}


【安卓笔记】Handler:10秒钟时间显示进度条
 

相关推荐