直接上代码
private int seconds = 30;//秒数
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.textView:
// FIXME: 发送验证码请求
displayToast("发送成功");
textView.post(runnable);//执行倒计时
}
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
textView.setText(seconds <= 0 ? "重新获取" : String.format(Locale.CHINA, "%ds", seconds));
textView.setEnabled(seconds <= 0);
seconds--;
if (seconds >= 0) {
textView.postDelayed(this, 1000);//递归执行
} else {
seconds = 30;//复位
}
}
};
网友评论