本文实例为大家分享了Android实现点击获取验证码60秒后重新获取的具体代码,供大家参考,具体内容如下
上代码
/**
* Created by Xia_焱 on 2017/5/7.
*/
public class CountDownTimerUtils extends CountDownTimer {
private TextView mTextView;
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.mTextView = textView;
}
@Override
public void onTick(long millisUntilFinished) {
mTextView.setClickable(false); //设置不可点击
mTextView.setText(millisUntilFinished / 1000 + "秒后可重新发送"); //设置倒计时时间
mTextView.setBackgroundResource(R.drawable.bg_identify_code_press); //设置按钮为灰
SpannableString spannableString = new SpannableString(mTextView.getText().toString());
ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.setText(spannableString);
}
@Override
public void onFinish() {
mTextView.setText("重新获取验证码");
mTextView.setClickable(true);//重新获得点击
mTextView.setBackgroundResource(R.drawable.bg_identify_code_normal);
}
}
TextView背景色
bg_identify_code_press
<?xml version="1.0" encoding="utf-8"?
<shape xmlns:android="http://schemas.android.com/apk/res/android"
<solid android:color="#C0C0C0" / <!--填充色 透明--
<corners android:radius="7dp" / <!-- 圆角 --
</shape
bg_identify_code_normal
<?xml version="1.0" encoding="utf-8"?
<shape xmlns:android="http://schemas.android.com/apk/res/android"
<solid android:color="#2BAF2B" / <!--填充色 透明--
<corners android:radius="7dp" / <!-- 圆角 --
</shape
布局代码
<TextView
android:id="@+id/tv_yzm"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_height="45dp"
android:background="@drawable/bg_identify_code_normal"
android:gravity="center"
android:text="点击获取验证码"
android:textColor="#FFF"
android:textSize="15dp" /
项目代码
private void initView() {
tv_yzm = (TextView) findViewById(R.id.tv_yzm);
tv_yzm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tv_yzm, 60000, 1000);
mCountDownTimerUtils.start();
}
});
}
效果图
这里写图片描述
以上就是本文的全部内容,希望对大家的学习有所帮助。
更多Android进阶指南 可以扫码 解锁 《Android十大板块文档》

1.Android车载应用开发系统学习指南(附项目实战)
2.Android Framework学习指南,助力成为系统级开发高手
3.2023最新Android中高级面试题汇总+解析,告别零offer
4.企业级Android音视频开发学习路线+项目实战(附源码)
5.Android Jetpack从入门到精通,构建高质量UI界面
6.Flutter技术解析与实战,跨平台首要之选
7.Kotlin从入门到实战,全方面提升架构基础
8.高级Android插件化与组件化(含实战教程和源码)
9.Android 性能优化实战+360°全方面性能调优
10.Android零基础入门到精通,高手进阶之路
敲代码不易,关注一下吧。ღ( ´・ᴗ・` ) 🤔