昨天刚写了一个类似这样的功能,今天看博客 看到同类功能的实现 且更简单高效 android内部封装了类似的类个方法 拿来主义 直接用。
Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView send=(TextView)findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CountDownTimerUtil timerUtil=new CountDownTimerUtil(send,5000,1000);
timerUtil.start();
}
});
}
CountDownTimer
public class CountDownTimerUtil extends CountDownTimer {
private TextView textView;
/**
* TextView view 自己新家构造方法参数 获取要操作的控件
* long millisInFuture 间隔时长 该参数传给OnTick方法
* long countDownInterval 多长时间调用一次onTick 方法
*/
public CountDownTimerUtil(TextView view,long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.textView=view;
}
@Override
public void onFinish() {
textView.setEnabled(true);
textView.setText("获取短信验证码");
}
@Override
public void onTick(long l) {
textView.setEnabled(false);
textView.setText(l/1000+"秒可以发送");
}
}