效果图
布局
//倒计时秒
<TextView
android:id="@+id/time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="5秒"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="45dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="45dp"
android:gravity="center"
android:textSize="40dp"
android:textColor="#f00"/>
MainActivity
package com.bawei.ye.yue1.ui;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.bawei.ye.yue1.R;
public class MainActivity extends AppCompatActivity {
private TextView time;
private int i =5;
//handeler倒计时
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (i==0){
//跳转
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
i=-1;
startActivity(intent);
//结束倒计时页面
finish();
}else {
time.setText(i+"秒");
i--;
handler.sendEmptyMessageDelayed(0,1000);
}
}
};
private SharedPreferences sp;
private SharedPreferences.Editor edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
time = findViewById(R.id.time);
handler.sendEmptyMessageDelayed(0,1000);
//sp
sp = getSharedPreferences("登录一次", MODE_PRIVATE);
if (sp.getBoolean("登录一次",false)){
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
i=-1;
startActivity(intent);
finish();
return;
}
//获取编译器
edit = sp.edit();
//存值
edit.putBoolean("登录一次",true);
//提交事务
edit.commit();
}
}