效果圖:
實現思路,利用 CountDownTimer 倒計時工具類,指定間隔修改button顯示文字以及是否可以點擊,代碼如下:
//下面代碼直接寫在onclick事件中
CountDownTimer timer = new CountDownTimer(60000, 1000) {//參數爲 (倒計時長,間隔)
@Override
public void onTick(long millisUntilFinished) {
bnSendCode.setEnabled(false);
bnSendCode.setText("已發送(" + millisUntilFinished / 1000 + ")");
}
@Override
public void onFinish() {
bnSendCode.setEnabled(true);
bnSendCode.setText("重新發送");
}
}.start();