安卓 Timer 定时器并更新界面
ITPOW2018/4/8 16:56:32
final int[] total = { 60 }; Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { total[0] -= 1; runOnUiThread(new Runnable() { @Override public void run() { ((TextView)findViewById(R.id.randomCodeBtn)).setText(total[0] + "秒后获取"); } }); } }; timer.schedule(task, 1000, 1000);
更新了 total,所以除了使用 final,还要使用数组,否则不能改变 total 的值。
由于是在 TimerTask 中更新界面,所以须使用 runOnUiThread。
<<返回首页<<