简单倒计时,无点击倒计时跳转(倒计时五秒):
1.handler倒计时
private TextView tv_num; //xml里面的id
//数字数量为4
private int count=4;
private String[] str={"一","二","三","四"};
//接受信息
private Handler handler = new Handler(new Handler.Callback(){
@Override
public boolean handleMessage(@NonNull Message msg) {
tv_num.setText(str[count]);
//如果数字为一跳转到列表页面
if(tv_num.getText().toString().equals("一")){
Intent intent = new Intent(MainActivity.this,ListViewActivity.class);
startActivity(intent);
}
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tv_num = (TextView) findViewById(R.id.tv_num);
//创建线程
new Thread(new Runnable() {
@Override
public void run() {
while (count>0){
try {
//线程休眠一秒
Thread.sleep(1000);
//发送消息
handler.sendEmptyMessage(0);
count--;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
2.timer倒计时
private int time = 2;
@Override
protected void initView() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
time--;
if(time==0){
startActivity(new Intent(SplashActivity.this,HomeActivity.class));
finish();
}
}
});
}
}, 0, 1000);
}
*仅供参考,后续更新
458

被折叠的 条评论
为什么被折叠?



