常用程式碼整理:執行緒非同步操作
阿新 • • 發佈:2018-12-18
說明:大部分內容都是參考別的文章,這裡做整理是為了以後的程式設計有實用的模板,可以即需即用。
1、倒計時
public class RegisterActivity extends BaseActivity {
private TimeCount mTimeCount;
...
mTvGetVerifyCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTimeCount = new TimeCount(59000, 1000);
mTimeCount.start();
}
});
...
@Override
protected void onDestroy() {
super.onDestroy();
if (mTimeCount != null) {
mTimeCount.cancel();
}
}
...
class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
mTvGetVerifyCode.setClickable(false);
mTvGetVerifyCode.setText (millisUntilFinished / 1000 + "s");
}
@Override
public void onFinish() {
mTvGetVerifyCode.setText("獲取驗證碼");
mTvGetVerifyCode.setClickable(true);
}
}
}