原理是这样,我们在SharedPreferences中存储一个int型数据,用来代表第几次登录,每次启动时都读取出来判断是不是第一次启动,然后依次判断是否要显示欢迎界面,

具体实现如下:

设置一个欢迎界面的Activity,并设置为主Activity,在判断第几次启动后来决定要不要跳转到MainActivity

package com.example.f;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class StartActivity extends AppCompatActivity {
private Button go=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start); go=(Button)findViewById(R.id.go);
SharedPreferences userInfo = getSharedPreferences("start", MODE_PRIVATE);
SharedPreferences.Editor editor = userInfo.edit();
Int x;
//获取记录启动次数的值,若获取不到就默认为1
x=userInfo.getInt("start",1);
//判断第几次启动
if(x==1)
{
//为启动数加一
x++;
editor.putInt("start",x);
editor.commit(); }
else {
//若不是第一次登录就直接跳转MainActivity
x++;
editor.putInt("start",x);
editor.commit();
Intent it=new Intent();
it.setClass(StartActivity.this,MainActivity.class);
startActivity(it);
StartActivity.this.finish();
}
//欢迎界面进入应用的按钮
go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it=new Intent();
it.setClass(StartActivity.this,MainActivity.class);
startActivity(it);
StartActivity.this.finish(); }
});
}
}

  布局文件只有一个按钮

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity"> <Button
android:id="@+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

  初次启动效果如下

最新文章

  1. 如何利用pt-online-schema-change进行MySQL表的主键变更
  2. spring 声明式事务管理
  3. C#基础入门一
  4. Location 对象
  5. 调用存储过程从EntityFramework
  6. 基于go-ceph创建CEPH块设备及快照
  7. Dubbo_Admin安装
  8. 开源DBCP、C3P0、Proxool 、 BoneCP连接池的比较
  9. 常用js函数
  10. C++,对象成员的访问
  11. Longest Uncommon Subsequence I
  12. Mybatis 入门之resultMap与resultType解说实例
  13. Js/如何修改easyui修饰的input的val值
  14. Event Recommendation Engine Challenge分步解析第七步
  15. axis 数据流
  16. npm 是干什么的
  17. django之signal机制再探
  18. dup2替换
  19. Asp.Net_HttpModule的应用
  20. 【python3】 enumerate用法总结(转)

热门文章

  1. ios启动流程
  2. maven项目pom.xml加载本地jar,自定义jar
  3. 深入理解JDK中的Reference原理和源码实现
  4. C语言指针及占据内存空间
  5. html网页压缩保存到数据库,减少空间占用,实现过程遇到的解压问题
  6. tf识别非固定长度图片ocr(数字+字母 n位长度可变)- CNN+RNN+CTC
  7. 用TreeWalk提高网速及其在vista中的安装方法
  8. 2019牛客多校2 H Second Large Rectangle(悬线法)
  9. Spring事务中的隔离级别
  10. python练习——第0题