android 计时器

    xiaoxiao2021-03-25  158

    import android.os.Handler; import android.os.Message; import android.widget.Button; import java.util.Timer; import java.util.TimerTask; /** * Created by ${hang.liu} on 2017/3/8. */ public class TimeDown { private int times; private long delay; private Button mtv; private long period; private int time2; public TimeDown(int times, long delay, Button mtv, long period) { this.times = times; this.delay = delay; this.mtv = mtv; this.period = period; time2=times; } private Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); int what = msg.what; switch (what) { case 1: mtv.setText("" + times); break; case 2: times=time2; mtv.setText("重新获取验证码"); mtv.setClickable(true); break; } } }; public void timerDown(){ mtv.setClickable(false); final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { times--; if (times >= 0) { mHandler.sendEmptyMessage(1); } else { timer.cancel(); mHandler.sendEmptyMessage(2); return; } } }, delay, period); } }
    转载请注明原文地址: https://ju.6miu.com/read-1323.html

    最新回复(0)