Android 倒计时的实现

该方法参考了以下文章 

http://blog.csdn.net/ithomer/article/details/6903084


public class timerTask extends Activity{
	private int recLen = 0;
	private TextView txtView;

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);

		setContentView(R.layout.timertask);
		txtView = (TextView)findViewById(R.id.txttime);
		
		handler.postDelayed(runnable, 1000);
	}	

	Handler handler = new Handler();
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			recLen++;
			txtView.setText("" + recLen);
			handler.postDelayed(this, 1000);
		}
	};
}
以下方法为自己整理的

CountDownTimer countDownTimer = new CountDownTimer(8000, 1000) {
			// millisUntilFinished:表示剩余的时间
			@Override
			public void onTick(long millisUntilFinished) {
				textView1.setText("seconds remaining: " + millisUntilFinished / 1000 + 1);

			}

			// 计时结束时的操作
			@Override
			public void onFinish() {
				textView1.setText("Bomb !!!!!");
			}
		};
		countDownTimer.start();



你可能感兴趣的:(Android 倒计时的实现)