仿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=