欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

验证码倒计时 验证码倒计时验证码倒计时 

程序员文章站 2022-06-23 18:10:03
...

在我们注册或者修改信息的时候,常会用到60s倒计时这个功能,写了这篇文章,大家共享一下:

效果图:

           验证码倒计时 
            
    
    
        验证码倒计时验证码倒计时 

直接上代码:

activity.java

public class MainActivity extends Activity {

	private TimeCount mTiemTimeCount;
	private TextView tv_code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mTiemTimeCount = new TimeCount(60000, 1000);
		initView();

	}

	private void initView() {
		// TODO Auto-generated method stub
		tv_code = (TextView) findViewById(R.id.verify_code);
		tv_code.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mTiemTimeCount.start();
			}
		});
	}

	// 计时重发
	private class TimeCount extends CountDownTimer {

		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			tv_code.setClickable(false);
			tv_code.setText(millisUntilFinished / 1000 + "秒后重新发送");
		}

		@Override
		public void onFinish() {
			tv_code.setText("获取验证码");
			tv_code.setClickable(true);
		}
	}
	@Override
	protected void onDestroy() {
	    super.onDestroy();
	    mTiemTimeCount.cancel();
	}
}

 

mTiemTimeCount = new TimeCount(60000, 1000);可以自己更改时间

 

     源码点击下载