安卓第十天---载入页面+引导页+SharedPreferences保存密码以及自动登录的模拟演示

本文介绍了如何模拟安卓应用的启动过程,包括加载页面、引导页和自动登录功能。通过SharedPreferences保存密码,实现首次登录后自动跳转。引导页使用Thread线程控制倒计时,并依据SharedPreferences判断是否为首次启动。最后提供了源代码供读者参考学习。
摘要由CSDN通过智能技术生成

仿app的开始载入+引导页+自动登录
首先解释一下图片的内容
在这里插入图片描述

第一次打开这个app(我从AS直接安装),因为是第一次打开,所以流程是加载页面->引导页->登录页面->填入账号密码,然后点击登录(这里没有设置Intent事件,所以我用一个Toast来表示操作成功)
然后关闭这个app
第二次打开开始直接进入加载页面,但是由于在第一次登录的时候是设置了自动登录,以及保存密码,所以在载入页面完成之后直接跳转到登录页,由于我在登录页设置了如果账号密码正确,自动登录,所以我用Intent让自动登陆后的页面跳转到引导页
上面就是对这个demo的流程的一个解释
下面我们开始贴代码:
引导页布局

<android.support.constraint.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">

<ImageView
    android:id="@+id/transition_image"
    android:src="@mipmap/transition_0"
    android:scaleType="centerCrop"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TextView
    android:id="@+id/left_time"
    android:layout_width="65dp"
    android:layout_height="45dp"
    android:layout_marginEnd="16dp"
    android:background="@drawable/timing_bg"
    android:gravity="center"
    android:text="5秒"
    android:textColor="@color/white"
    app:layout_constraintEnd_toEndOf="parent"
    tools:layout_editor_absoluteY="16dp" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:text="@string/copyright"
    android:gravity="center"
    android:textColor="@color/white"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/transition_image" />

 </android.support.constraint.ConstraintLayout>

引导页代码以及跳转逻辑
这里引导页是用一个Thread线程来控制上面倒计时的数字,在倒计时为0的时候进行一个跳转,如果是第一次打开app,让它跳转到引导页,如果不是第一次登录,就让他跳转到登录页
—判断是否为第一次打开用一个Sharedperferences来记录

public class MainActivity extends AppCompatActivity {
TextView timingLeft;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timingLeft = findViewById(R.id.left_time);
    timingLeft.getBackground().setAlpha(222);
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                for(int i = 5; i >= 0; i--){
                    Thread.sleep(1000);
                    timingLeft.setText(i + "秒");
                    if (i == 0){
                        Looper.prepare();
                        isFirstOpen();
                        Looper.loop();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();

}

private void isFirstOpen(){//判断是否为第一次打开,然后进行操作
    SharedPreferences setting = getSharedPreferences("isOpened",0);
    Boolean isFirst = setting.getBoolean("IsFirst",true);
    if (isFirst){
        //如果是第一次登录,将IsFirst设置为false,然后开启引导页
        setting.edit().putBoolean("IsFirst",false).commit();
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,GuidPageActivity.class);
        startActivity(intent);
    }else{
        //如果不是第一次打开,直接跳转登录界面
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,LoginActivity.class);
        startActivity(intent);
    }

}

}

然后引导页布局

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
    android:id="@+id/guid_page_view"
    android:background="@color/colorPrimary"
    android:layout_width=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值