标签:
简述:
CountDownTimer类用来实现倒计时效果。
构造方法接收两个参数,第一个设置倒计时长度,第二个设置倒计时间隔。
用此方法时只需复写onTick()和onFinish()这两个方法即可。
示例:
package com.example.androidtest;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
//Data fields
private Button button;
private CountDownTimer downTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
//Set the click listener for the button
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
downTimer.start();
}
});
//Create a count down timer of 60 seconds time and 1 second interval
downTimer=new CountDownTimer(60000,1000) {
//This method set the behavior of ticked
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
button.setBackgroundColor(Color.parseColor("#B6B6D8"));
button.setClickable(false);
button.setText(millisUntilFinished / 1000 + "秒后可重新发送");
}
//This method set the behavior of finished
@Override
public void onFinish() {
// TODO Auto-generated method stub
button.setText("重新获取验证码");
button.setClickable(true);
button.setBackgroundColor(Color.parseColor("#4EB84A"));
}
};
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/qq_16912257/article/details/47323885