笔记之SharedPreferences存储轻量级数据基本运用
备注: 本文参考了CSDN上某博主的一篇文章 -----基于SP(SharedPreferences)的基本使用以及实际应用介绍 链接: https://blog.csdn.net/qq_37842258/article/details/70751726
1.SharedPreferences 是android中比较轻量级的存储数据类:
主要保存一些常用的配置,如用户登陆时勾选了记住密码,则保存账号&密码, 下次进入程序时不用再输入;
还可以在用户安装完某app后,根据保存的是否是第一次使用此app的状态,来决定进去app时,是否要加载导航页给用户;
2.SharedPreferences的存储数据:
//第一个参数是存储的文件的名字,存储的文件是xml格式的,路径/data/data/<包名>/shared_prefs/xxxx.xml
//第二个参数是文件读写类型,MODE_PRIVATE是私有化,只能被本应用读写,外部程序不能访问,保证了安全性
//SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现
//editor无论是存储、移除、还是清除数据,最后一定都要记得提交
SharedPreferences sp = getSharedPreferences("LoginSettings",MODE_PRIVATE);
SharedPreferences.Editor editor = getSharedPreferences("LoginSettings",MODE_PRIVATE);
editor.putBoolean("IfRemember",true);
editor.putString("Name",str_name);
editor.putString("PWD",str_password);
//editor.commit()// 同步
editor.apply() //异步
commit和apply都可以用来提交,具体差异看网上有的大神做了分析, 文章 "每日一问:谈谈 SharedPreferences 的 apply() 和 commit()" 链接https://www.cnblogs.com/liushilin/p/11153041.htmlde
3.SharedPreferences的读取数据:
SharedPreferences sp = getSharedPreferences("LoginSettings",MODE_PRIVATE);
String name = sp.getString("Name","请输入姓名")
String password = sp.getString("PWD","请输入密码")
下面是一个登录时利用SharedPreference记住密码的demo
package com.example.sharedpreference_demo; import androidx.appcompat.app.AppCompatActivity; 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; /* 是否是 1.第一次登陆, 是否勾选记住密码 勾选,将账号&密码存储后,登陆 不勾选,直接登陆 2.之后登陆, 如果之前勾选了记住密码,则直接从存储数据中读取账号密码,并设置输入框 如果之前没勾选,则手动输入 */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_Name; private EditText et_PSW; private CheckBox checkBox; private Button btn_Login; Boolean if_remember; private SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_Name = findViewById(R.id.et_name); et_PSW = findViewById(R.id.et_psw); checkBox =findViewById(R.id.checkbox_remember); btn_Login = findViewById(R.id.btn_login); btn_Login.setOnClickListener(this); SharedPreferences sp = getSharedPreferences("LoginSettings",MODE_PRIVATE); if_remember = sp.getBoolean("IfRemember",false); if(if_remember){ et_Name.setText(sp.getString("Name","请输入姓名")); et_PSW.setText(sp.getString("PWD","请输入密码")); checkBox.setChecked(true); } } private void Login(){ String str_name = et_Name.getText().toString().trim(); String str_password = et_PSW.getText().toString().trim(); if(TextUtils.isEmpty(str_name)||(TextUtils.isEmpty(str_password))){ Toast.makeText(MainActivity.this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show(); return; } SharedPreferences sp = getSharedPreferences("LoginSettings",MODE_PRIVATE); editor = sp.edit(); if(checkBox.isChecked()){ editor.putBoolean("IfRemember",true); editor.putString("Name",str_name); editor.putString("PWD",str_password); } else{ editor.clear(); } editor.apply(); Toast.makeText(MainActivity.this,"登陆中...",Toast.LENGTH_SHORT).show(); } @Override public void onClick(View v) { Login(); } }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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" tools:context=".MainActivity"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="用户名" android:textSize="16sp" app:layout_constraintBottom_toBottomOf="@id/et_name" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/et_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="32dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/et_psw" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tv_psw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="密码" android:textSize="16sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tv_name" /> <EditText android:id="@+id/et_psw" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginLeft="32dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/tv_psw" app:layout_constraintTop_toBottomOf="@id/et_name" /> <CheckBox android:id="@+id/checkbox_remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="64dp" android:layout_marginLeft="64dp" android:text="记住密码" app:layout_constraintBottom_toBottomOf="@+id/btn_login" app:layout_constraintEnd_toStartOf="@+id/btn_login" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tv_psw" app:layout_constraintVertical_bias="1.0" /> <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_marginEnd="64dp" android:layout_marginRight="64dp" android:text="登陆" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/checkbox_remember" app:layout_constraintTop_toBottomOf="@id/et_psw" /> </androidx.constraintlayout.widget.ConstraintLayout>

浙公网安备 33010602011771号