倒计时按钮CountDownTimer

在网上闲逛时发现了几篇写得好的文章,总结了一下内容分享出来

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 >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值