Android获取验证码倒计时

package com.example.countdowntest;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button bt_1;
	private TextView tv_1;
	private TimeCount time;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		time = new TimeCount(6000, 1000);// 构造CountDownTimer对象
		bt_1 = (Button) findViewById(R.id.bt_1);
		tv_1 = (TextView) findViewById(R.id.tv_1);

		bt_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				time.start();// 开始计时
			}
		});

	}

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			bt_1.setText("重新验证");
			bt_1.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			bt_1.setClickable(false);
			tv_1.setText(millisUntilFinished / 1000 + "");
		}
	}

}

 
 

你可能感兴趣的:(Android)