获取验证码倒计时功能
(1)布局文件中就是一个按钮获取验证码
<Button
android:id="@+id/btnsms"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginRight="5dp"
android:background="@color/blue_bg"
android:textColor="@color/white"
android:text="获取验证码" />
(2)代码实现逻辑
btCode = (Button) findViewById(R.id.btnsms);
btCode.setOnClickListener(this);
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnsms:
new TimeCount(60 * 1000, 1000).start();
break;
default:
break;
}
}
private class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
btCode.setClickable(false);
btCode.setText(millisUntilFinished / 1000 + "秒后可重发");
}
@Override
public void onFinish() {
btCode.setText("重新获取");
btCode.setClickable(true);
}
}
以上步骤可以实现倒计时功能(60秒)