ImageView img;
TextView tv;
SharedPreferences sharedPreferences;
int time=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
handler.sendEmptyMessageDelayed(1,1000);
//默认第二次不显示
sharedPreferences = getSharedPreferences("qmy", MODE_PRIVATE);
boolean isfinish = sharedPreferences.getBoolean("isfinish", false);
if (isfinish){
startActivity(new Intent(MainActivity.this,show.class));
finish();
}
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
time--;
if (time<=0){
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putBoolean("isfinish",true);
edit.commit();
startActivity(new Intent(MainActivity.this,show.class));
}else {
tv.setText(time+"");
handler.sendEmptyMessageDelayed(1,1000);
}
}
};
@Override
protected void onDestroy() {
if (handler!=null) {
handler.removeMessages(1);
handler=null;
}
super.onDestroy();
}
倒计时第二次不显示
最新推荐文章于 2024-06-03 05:03:41 发布