開始寫吧今天是2018/8/9 昨天突然網(wǎng)連不上了
好了會歸正題
先上一個效果圖
首先寫一個類來繼承 CountDownTimer算了還是直接復制代碼吧(直接點大家都是程序員誰不知道誰呀)
/**
* 創(chuàng)建人: Nine tails fox
* 創(chuàng)建時間: 2018/8/9 12:00
* 功能描述:功能
* 聯(lián)系方式:1037438704@qq.com
*
* @author dell-pc
*/
public class VerificationTimerextends CountDownTimer {
private TextViewtextView;
? ? private int one, tow;
/**
? ? * @author millisInFuture 一共的時間
? ? * @author countDownInterval 幾秒
? ? * @author textView 控件可以更換你所需要的控件
? ? * @author one onTick方法中的背景顏色? ? ? ? 這兩個都是用于控制控件背景的
? ? * @author tow? onFinish 方法中的背景顏色
* */
? ? public VerificationTimer(long millisInFuture, long countDownInterval, TextView textView) {
super(millisInFuture, countDownInterval);
? ? ? ? this.textView = textView;
? ? }
public VerificationTimer(long millisInFuture, long countDownInterval, TextView textView, int one, int tow) {
super(millisInFuture, countDownInterval);
? ? ? ? this.textView = textView;
? ? ? ? this.one = one;
? ? ? ? this.tow = tow;
? ? }
@Override
? ? public void onTick(long millisUntilFinished) {
//如果為空就不換背景顏色
? ? ? ? if (one !=0) {
textView.setBackgroundResource(one);
? ? ? ? }
textView.setText(millisUntilFinished /1000 +"s");
? ? ? ? textView.setEnabled(false);
? ? }
@Override
? ? public void onFinish() {
//如果為空就不換背景顏色
? ? ? ? if (tow !=0) {
textView.setBackgroundResource(tow);
? ? ? ? }
textView.setEnabled(true);
? ? ? ? textView.setText("獲取驗證碼");
? ? }
}
上面的是封裝的很簡單
下面是使用方法
public class MainActivityextends AppCompatActivity {
private TextViewtextView;
? ? private VerificationTimerverificationTimer;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? initView();
? ? }
private void initView() {
textView = findViewById(R.id.textView);
//? ? ? ? verificationTimer = new VerificationTimer(5000, 1000, textView, R.drawable.bg_shape_false, R.drawable.bg_shape_true);
? ? ? ? verificationTimer =new VerificationTimer(5000, 1000, textView);
? ? ? ? textView.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? public void onClick(View v) {
verificationTimer.start();
? ? ? ? ? ? }
});
? ? }
}