第一步:先画布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.zyl.t3.MainActivity">
<ImageView
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/b"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
第二步:Java部分
public class MainActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGHT = 3000; // 延迟3秒
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = getSharedPreferences("phone", Context.MODE_PRIVATE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (preferences.getBoolean("firststart", true)) {
editor = preferences.edit();
//将登录标志位设置为false,下次登录时不在显示首次登录界面
editor.putBoolean("firststart", false);
editor.commit();
//实现跳转
Intent intent=new Intent();
intent.setClass(MainActivity.this,Register.class);
MainActivity.this.startActivity(intent);
MainActivity.this.finish();
}else{
Intent intent=new Intent();
intent.setClass(MainActivity.this,Register.class);
MainActivity.this.startActivity(intent);
MainActivity.this.finish();
}
}
},SPLASH_DISPLAY_LENGHT);
}
}