在网上闲逛时发现了几篇写得好的文章,总结了一下内容分享出来
1.自定义类实现CountDownTimer
public class TimeCount extends CountDownTimer{
private TextView textView;
public TimeCount(long millisInFuture, long countDownInterval,TextView textView) {
super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
this.textView=textView;
}
@Override
public void onFinish() {//计时完毕时触发
textView.setText("获取验证码");
textView.setClickable(true);
textView.setBackgroundResource(R.drawable.textview_round_angle);; //还原背景色
}
@Override
public void onTick(long millisUntilFinished){//计时过程显示
textView.setClickable(false);
textView.setText(millisUntilFinished /1000+"秒后重发");
textView.setBackgroundResource(R.drawable.textview_round_angle_gray);
SpannableString spannableString = new SpannableString(textView.getText().toString()); //获取按钮上的文字
ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为红色
textView.setText(spannableString);
}
}
2.在代码初始化对象
TimeCoutn time = new TimeCount(60000,1000,textView);
3.在TextView的点击事件中调用
time.start();
4.1灰色圆角
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- view背景色 -->
<solid android:color="@color/colorGray" />
<!-- 边框颜色 宽度 -->
<stroke
android:width="1dip"
android:color="@color/colorGray" />
<!-- 边框圆角 -->
<corners
android:bottomRightRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"/>
</shape >
4.2绿色圆角
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- view背景色 -->
<solid android:color="@color/colorGreen" />
<!-- 边框颜色 宽度 -->
<stroke
android:width="1dip"
android:color="@color/colorGreen" />
<!-- 边框圆角 -->
<corners
android:bottomRightRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"/>
</shape >