Welcome.xml布局
- 布局构思比较随意。在垂直的线性布局右上角合适位置放置一个"跳过"文本框即可
Welcome.java
- 进行相关的获取控件,此处不一 一做介绍,直接上代码+简单注释
- 这里的Timer其实是一个调度器
Timer timer = new Timer();
private Handler handler;
private Runnable runnable;
private int tiaoguo_time = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
timer.schedule(task, 0, 1000);
handler = new Handler();
handler.postDelayed(runnable = new Runnable() {
@Override
public void run() {
Intent intent = new Intent(welcome.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 5000);
}
- 还需要使用到多线程的TimerTask这个抽象类,它实现了Runnable接口,所以具备了多线程的能力。
- 这里直接实现一个TimerTask(当然,你可以实现多个TimerTask,多个TimerTask可以被一个Timer会被分配到多个Timer中被调度,Timer的实现机制其实就是内部的调度机制)
- 然后再去编写run方法,每秒执行一次,当然你通过一个timer对象来操作多个timerTask,其实timerTask本身没什么意义,只是和timer集合操作的一个对象,实现它就必然有对应的run方法,以被调用
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
tiaoguo_time--;
tiaoguo.setText(tiaoguo_time + "s 跳 过 ");
if (tiaoguo_time < 1) {
timer.cancel();
tiaoguo.setVisibility(View.GONE);
}
}
});
}
};
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tiaoguo:
Intent intent = new Intent(welcome.this, MainActivity.class);
startActivity(intent);
finish();
if (runnable != null) {
handler.removeCallbacks(runnable);
}
break;
default:
break;
}
}
效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/aa3cd130d81c19ddd4f06076ddeffe99.jpeg)