package cn.zsc.demo02;
import android.app.Activity;
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 {
private Button checking;
private TimeCount time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checking = (Button) findViewById(R.id.button1);
time = new TimeCount(60000, 1000);
checking.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
time.start();// 开始计时
}
});
}
class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
}
@Override
public void onFinish() {// 计时完毕时触发
checking.setText("重新验证");
checking.setClickable(true);
}
@Override
public void onTick(long millisUntilFinished) {// 计时过程显示
checking.setClickable(false);
checking.setText(millisUntilFinished / 1000 + "秒");
}
}
}
import android.app.Activity;
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 {
private Button checking;
private TimeCount time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checking = (Button) findViewById(R.id.button1);
time = new TimeCount(60000, 1000);
checking.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
time.start();// 开始计时
}
});
}
class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
}
@Override
public void onFinish() {// 计时完毕时触发
checking.setText("重新验证");
checking.setClickable(true);
}
@Override
public void onTick(long millisUntilFinished) {// 计时过程显示
checking.setClickable(false);
checking.setText(millisUntilFinished / 1000 + "秒");
}
}
}