android 获取短信验证码倒计时
public class MainActivity extends Activity {
private Button submit;
private TimeCount time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
time = new TimeCount(60000, 1000);
submit = (Button) findViewById(R.id.btn_login_submit);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
time.start();// 開始计时
}
});
}
class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {// 计时完成
submit.setText("获取验证码");
submit.setClickable(true);
}
@Override
public void onTick(long millisUntilFinished) {// 计时过程
submit.setClickable(false);//防止反复点击
submit.setText(millisUntilFinished / 1000 + "s");
}
}