Android_SharedPreferences实现的自动登录和记住密码
效果:
先贴一个SharedPreferences工具类:
package com.example.didida_corder.ToolClass; import android.content.Context; import android.content.SharedPreferences; import androidx.annotation.Nullable; import java.util.Map; public class SPUtils { /** * 保存在手机里的SP文件名 */ public static final String FILE_NAME = "my_sp"; /** * 保存数据 */ public static SPUtils put(Context context, String key, Object obj) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); if (obj instanceof Boolean) { editor.putBoolean(key, (Boolean) obj); } else if (obj instanceof Float) { editor.putFloat(key, (Float) obj); } else if (obj instanceof Integer) { editor.putInt(key, (Integer) obj); } else if (obj instanceof Long) { editor.putLong(key, (Long) obj); } else { editor.putString(key, (String) obj); } editor.commit(); SPUtils spUtils=new SPUtils(); return spUtils; } /** * 获取指定数据 */ public static Object get(Context context, String key, @Nullable Object defaultObj) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE); if (defaultObj instanceof Boolean) { return sp.getBoolean(key, (Boolean) defaultObj); } else if (defaultObj instanceof Float) { return sp.getFloat(key, (Float) defaultObj); } else if (defaultObj instanceof Integer) { return sp.getInt(key, (Integer) defaultObj); } else if (defaultObj instanceof Long) { return sp.getLong(key, (Long) defaultObj); } else if (defaultObj instanceof String) { return sp.getString(key, (String) defaultObj); } return null; } /** * 删除指定数据 */ public static void remove(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.remove(key); editor.commit(); } /** * 返回所有键值对 */ public static Map<String, ?> getAll(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE); Map<String, ?> map = sp.getAll(); return map; } /** * 删除所有数据 */ public static void clear(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.clear(); editor.commit(); } /** * 检查key对应的数据是否存在 */ public static boolean contains(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE); return sp.contains(key); } }
调用这些方法实现相应功能
在第一次登陆时将账号密码存储到偏好中,判断是否选中记住密码和自动登录,并相应存储起来。
SPUtils.put(getContext(),"username",editText.getText().toString()). put(getContext(),"password",editText1.getText().toString()); if (rb_rember.isChecked()) SPUtils.put(getContext(),"rember",1); else SPUtils.put(getContext(),"rember",0); if (rb_auto.isChecked()) SPUtils.put(getContext(),"auto",1); else SPUtils.put(getContext(),"auto",0);
在界面初始化时判断是否自动登录,并作相应操作
if (Integer.parseInt(SPUtils.get(getContext(),"auto",222).toString())==1){ btn_register.setVisibility(View.INVISIBLE); btn_log.setVisibility(View.INVISIBLE); iv_head.setVisibility(View.VISIBLE); tv_name.setVisibility(View.VISIBLE); }
在登录界面初始化时判断是否记住密码,并作相应操作
if (Integer.parseInt(SPUtils.get(getContext(),"rember",3).toString())==1){ editText.setText(SPUtils.get(getContext(),"username","111").toString()); editText1.setText(SPUtils.get(getContext(),"password","111").toString()); }