<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context="com.lindroid.countdowndemo.MainActivity"> <Button android:id="@+id/btn_captcha" android:layout_width="match_parent" android:layout_height="50dp" android:background="#c7c7c7" android:text="获取验证码" android:textAllCaps="false" android:textColor="@android:color/black" android:textSize="18sp" /> </RelativeLayout>
private CountTimer countTimer;
/**
* 点击按钮后倒计时
*/
class CountTimer extends CountDownTimer {
public CountTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
/**
* 倒计时过程中调用
*
* @param millisUntilFinished
*/
@Override
public void onTick(long millisUntilFinished) {
}
/**
* 倒计时完成后调用
*/
@Override
public void onFinish() {
}
}
CountTimer countTimer = = new CountTimer(10000, 1000);
@Override
protected void onDestroy() {
super.onDestroy();
countTimer.cancel();
}
CountTimer countTimer = new CountTimer(10000, 1000);
/**
* 点击按钮后倒计时
*/
class CountTimer extends CountDownTimer {
public CountTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
/**
* 倒计时过程中调用
*
* @param millisUntilFinished
*/
@Override
public void onTick(long millisUntilFinished) {
Log.e("Tag", "倒计时=" + (millisUntilFinished/1000));
btnCaptcha.setText(millisUntilFinished / 1000 + "s后重新发送");
//设置倒计时中的按钮外观
btnCaptcha.setClickable(false);//倒计时过程中将按钮设置为不可点击
btnCaptcha.setBackgroundColor(Color.parseColor("#c7c7c7"));
btnCaptcha.setTextColor(ContextCompat.getColor(context, android.R.color.black));
btnCaptcha.setTextSize(16);
}
/**
* 倒计时完成后调用
*/
@Override
public void onFinish() {
Log.e("Tag", "倒计时完成");
//设置倒计时结束之后的按钮样式
btnCaptcha.setBackgroundColor(ContextCompat.getColor(context, android.R.color.holo_blue_light));
btnCaptcha.setTextColor(ContextCompat.getColor(context, android.R.color.white));
btnCaptcha.setTextSize(18);
btnCaptcha.setText("重新发送");
btnCaptcha.setClickable(true);
}
}
public void onTick(long millisUntilFinished) {
//处理后的倒计时数值
int time = (int) (Math.round((double) millisUntilFinished / 1000) - 1);
btnCaptcha.setText(String.valueOf(time)+"s后重新发送");
//设置倒计时中的按钮外观
btnCaptcha.setClickable(false);//倒计时过程中将按钮设置为不可点击
btnCaptcha.setBackgroundColor(Color.parseColor("#c7c7c7"));
btnCaptcha.setTextColor(ContextCompat.getColor(context, android.R.color.black));
btnCaptcha.setTextSize(16);
}
int time = (int) (Math.round((double) millisUntilFinished / 1000) - 1);
//拼接要显示的字符串
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(String.valueOf(time));
sb.append("s后重新发送");
//字符“后”在字符串中的下标
int index = String.valueOf(sb).indexOf("后");
//给秒数和单位设置蓝色前景色
ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, android.R.color.holo_blue_dark));
sb.setSpan(colorSpan, 0, index, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
btnCaptcha.setText(sb);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有