有时切换页面时需要设置延迟效果,话不多说,具体方法如下:
private timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//跳转界面
toLogin();
}
},2*1000);
其中Timer是一个计时器,schedule方法具体为public void schedule(TimerTask task, long delay){…},包含两个参数:
- 第一个参数为TimerTask类,该类实现了Runnable接口,故在使用时要重写run()方法;
- 第二个参数表示延迟时间,2*1000代表延迟两秒再执行run方法里面的动作。