如图所示的效果相信大家都不陌生,我们可以使用很多种方法去实现此效果,这里自己采用CountDownTimer
定时器简单封装下此效果,方便我们随时调用。
首页先在attrs.xml
中定义下所需的几个属性:
1
2
3
4
5
6
7
8
|
< resources > < declare-styleable name = "CountDownButton" > < attr name = "millisinfuture" format = "integer" /> < attr name = "countdowninterva" format = "integer" /> < attr name = "normalColor" format = "color" /> < attr name = "countDownColor" format = "color" /> </ declare-styleable > </ resources > |
下面是实现的具体代码,很简单的一种方式,通俗易懂:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
/** * Created by xiaolong on 2018/1/12. */ @SuppressLint ( "AppCompatCustomView" ) public class CountDownButton extends Button{ //总时长 private long millisinfuture; //间隔时长 private long countdowninterva; //默认背景颜色 private int normalColor; //倒计时 背景颜色 private int countDownColor; //是否结束 private boolean isFinish; //定时器 private CountDownTimer countDownTimer; public CountDownButton(Context context) { this (context, null ); } public CountDownButton(Context context, AttributeSet attrs) { this (context, attrs, 0 ); } public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) { super (context, attrs, defStyleAttr); TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CountDownButton,defStyleAttr, 0 ); //设置默认时长 millisinfuture = ( long ) typedArray.getInt(R.styleable.CountDownButton_millisinfuture, 60000 ); //设置默认间隔时长 countdowninterva = ( long )typedArray.getInt(R.styleable.CountDownButton_countdowninterva, 1000 ); //设置默认背景色 normalColor = typedArray.getColor(R.styleable.CountDownButton_normalColor,android.R.color.holo_blue_light); //设置默认倒计时 背景色 countDownColor = typedArray.getColor(R.styleable.CountDownButton_countDownColor,android.R.color.darker_gray); typedArray.recycle(); //默认为已结束状态 isFinish = true ; //字体居中 setGravity(Gravity.CENTER); //默认文字和背景色 normalBackground(); //设置定时器 countDownTimer = new CountDownTimer(millisinfuture, countdowninterva) { @Override public void onTick( long millisUntilFinished) { //未结束 isFinish = false ; setText((Math.round(( double ) millisUntilFinished / 1000 ) - 1 ) + "秒" ); setBackgroundResource(countDownColor); } @Override public void onFinish() { //结束 isFinish = true ; normalBackground(); } }; } private void normalBackground(){ setText( "获取验证码" ); setBackgroundResource(normalColor); } public boolean isFinish() { return isFinish; } public void cancel(){ countDownTimer.cancel(); } public void start(){ countDownTimer.start(); } } |
一个简单的调用方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class MainActivity extends AppCompatActivity { private CountDownButton countDownButton; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); countDownButton = ((CountDownButton) findViewById(R.id.countDownButton)); countDownButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { //这里判断是否倒计时结束,避免在倒计时时多次点击导致重复请求接口 if (countDownButton.isFinish()) { //发送验证码请求成功后调用 countDownButton.start(); } } }); } @Override protected void onDestroy() { super .onDestroy(); if (!countDownButton.isFinish()) { countDownButton.cancel(); } } } |
这样一个简单的封装就结束了,过程很简单。这里主要是对CountDownTimer
的使用练习,之前工作中一直没有接触过这个类。