android--------验证码倒计时

在我们注册或者修改信息的时候,常会用到60s倒计时这个功能,写了这篇文章,大家共享一下:

效果图:

           

直接上代码:

activity.java

 1 public class MainActivity extends Activity {
 2 
 3     private TimeCount mTiemTimeCount;
 4     private TextView tv_code;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_main);
10         mTiemTimeCount = new TimeCount(60000, 1000);
11         initView();
12 
13     }
14 
15     private void initView() {
16         // TODO Auto-generated method stub
17         tv_code = (TextView) findViewById(R.id.verify_code);
18         tv_code.setOnClickListener(new OnClickListener() {
19 
20             @Override
21             public void onClick(View v) {
22                 // TODO Auto-generated method stub
23                 mTiemTimeCount.start();
24             }
25         });
26     }
27 
28     // 计时重发
29     private class TimeCount extends CountDownTimer {
30 
31         public TimeCount(long millisInFuture, long countDownInterval) {
32             super(millisInFuture, countDownInterval);
33         }
34 
35         @Override
36         public void onTick(long millisUntilFinished) {
37             tv_code.setClickable(false);
38             tv_code.setText(millisUntilFinished / 1000 + "秒后重新发送");
39         }
40 
41         @Override
42         public void onFinish() {
43             tv_code.setText("获取验证码");
44             tv_code.setClickable(true);
45         }
46     }
47     @Override
48     protected void onDestroy() {
49         super.onDestroy();
50         mTiemTimeCount.cancel();
51     }
52 }

 

mTiemTimeCount = new TimeCount(60000, 1000);可以自己更改时间  

 

代码简单可直接使用

源码下载: https://github.com/DickyQie/android-layout-controls

posted @ 2016-11-18 10:24  切切歆语  阅读(273)  评论(0编辑  收藏  举报