使用SP制作一个记住密码 自动登录的案例
初始化页面时,判断 记住密码 和 自动登录 打钩没,存储相应数据到SP 第2次第n次打开的时候,从SP取数据,看上次是否 勾选了记住密码和自动登录,如果勾选了,就回显数据
MainActivity
package com.example.sp; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity2 extends AppCompatActivity { SharedPreferences sp; private EditText et_name; private EditText et_pwd; private CheckBox cb_remeberpwd; private CheckBox cb_autologin; private Button bt_login; private Button bt_register; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); // 获取首选项 SP sp = getSharedPreferences("config", Context.MODE_PRIVATE); initView(); // 第二次打开的时候从SP获取数据,从sp获取数据,进行画面同步 boolean remeberpwd = sp.getBoolean("remeberpwd",false); boolean autologin = sp.getBoolean("autologin",false); //记住密码 if(remeberpwd){ // 获取sp中的name 和 pwd 并保存到EditText String name = sp.getString("name",null); String pwd = sp.getString("pwd", null); et_name.setText(name); et_pwd.setText(pwd); cb_remeberpwd.setChecked(true); } //自动登录 if (autologin){ cb_autologin.setChecked(true); // 模拟 自动登录 Toast.makeText(this,"自动登录了",Toast.LENGTH_SHORT).show(); } } private void initView() { // 找到控件 et_name = findViewById(R.id.et_name); et_pwd = findViewById(R.id.et_pwd); cb_remeberpwd = findViewById(R.id.cb_remeberpwd); cb_autologin = findViewById(R.id.cb_autologin); bt_login = findViewById(R.id.bt_login); bt_register = findViewById(R.id.bt_register); // 设置监听 MyOnClickListener l =new MyOnClickListener(); bt_login.setOnClickListener(l); bt_register.setOnClickListener(l); } private class MyOnClickListener implements View.OnClickListener{ @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt_register: break; case R.id.bt_login: // 登录操作 String name = et_name.getText().toString().trim(); String pwd = et_pwd.getText().toString().trim(); if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)){ Toast.makeText(MainActivity2.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();; }else{ // 记住密码 打勾没有 if(cb_remeberpwd.isChecked()){ // 用户名和密码要保存 同时记住密码的状态要保存 SharedPreferences.Editor editor = sp.edit(); editor.putString("name",name); editor.putString("pwd",pwd); editor.putBoolean("remeberpwd",true); editor.apply(); } // 自动登录 打勾没有 if(cb_autologin.isChecked()){ SharedPreferences.Editor editor =sp.edit(); editor.putBoolean("autologin",true); editor.apply(); } } break; } } } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" /> <EditText android:id="@+id/et_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:password="true" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_remeberpwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" /> <CheckBox android:id="@+id/cb_autologin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动登录" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/bt_register" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="注册" /> <Button android:id="@+id/bt_login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="登录" /> </LinearLayout> </LinearLayout>
实现效果如下
勾选了记住密码和自动登录后关闭应用再次打开 自动登录了文字提示出现 说明数据回显成功