public class CountDownTimerUtils extends CountDownTimer{ private TextView mTextView; private Resources mRes; public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval,Resources mRes) { super(millisInFuture, countDownInterval); this.mTextView = textView; this.mRes=mRes; } @Override public void onTick(long millisUntilFinished) { mTextView.setClickable(false); //设置不可点击 mTextView.setText(millisUntilFinished / 1000 + mRes.getString(R.string.regain_get_code_miao)); //设置倒计时时间 mTextView.setTextSize(17); mTextView.setBackgroundResource(R.drawable.button_allcorner_selected); //设置按钮为灰色,这时是不能点击的 SpannableString spannableString = new SpannableString(mTextView.getText().toString()); //获取按钮上的文字 ForegroundColorSpan span = new ForegroundColorSpan(Color.WHITE); spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为红色 mTextView.setText(spannableString); } @Override public void onFinish() { mTextView.setText(R.string.get_code); mTextView.setClickable(true);//重新获得点击 mTextView.setBackgroundResource(R.drawable.button_allcorner); //还原背景色 } }
倒计时定时器工具类
最新推荐文章于 2024-05-15 02:13:53 发布