android开始时欢迎界面,Android应用第一次启动时的欢迎界面制作

原理是这样,我们在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();

}

});

}

}

布局文件只有一个按钮

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">

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" />

初次启动效果如下

0c6c93fe08bf8e01ee72d20855ef4391.png

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值