Android 常用代码整理:线程异步操作

说明:大部分内容都是参考别的文章,这里做整理是为了以后的编程有实用的模板,可以即需即用。

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);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章