一:Android常用数据存储,一共有五种方式,分别是
1.SharedPreferences储存数据,
2.文件存储
3.SQLite数据存储
4.ContentProvider储存数据
5.网络存储数据

二:SharedPreferences介绍
1.用于存放一些类似登录的配置信息,保存数据
2.本质上是一个xml文件,通过类似键值对的方式存放信息
3.可以在data/data/包名/shared_prefs,查看保存的内容

三:SharedPreferences操作模式
1.MODE_APPEND:追加方式存储
2.MODE_PRIVATE:私有方式储存,其他应用无法访问
3.MODE_WORLD_READABLE:可被其他应用读取
4.MODE_WORLD_WRITEABLE:可被其他应用写入
四:效果
1.保存效果,在data/data/包名,文件下找到myshare.xml

2.打开后,内容如下

2.读取效果(记住)

五:使用
1.新建ShareActivity代码

package com.hanjie.datastorage_2021_0414;import android.content.SharedPreferences;import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class ShareActivity extends AppCompatActivity {private EditText accEdt,pwdEdt;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_share);accEdt = findViewById(R.id.acc_edt);pwdEdt = findViewById(R.id.pwd_edt);//读取//1.获取SharedPreferences对象(参数1:文件名,参数2:模式)SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);//2.参数1:是key,参数2:当对应的key不存在时,返回参数2作为默认值String accStr = share.getString("account","");String pwdStr = share.getString("pwd","");accEdt.setText(accStr);pwdEdt.setText(pwdStr);findViewById(R.id.login_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//1.获取两个输入框的内容String account = accEdt.getText().toString();String pwd = pwdEdt.getText().toString();//2.验证if (account.equals("hanjie")&&pwd.equals("hanjie")){//2.1储存信息//1.获取SharedPreferences对象;参数一:存放的文件名,参数二:以什么样的模式SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);//2.获取Editor对象SharedPreferences.Editor edt = share.edit();//3.储存信息edt.putString("account",account);edt.putString("pwd",pwd);//4.指定提交操作edt.apply();//edt.commit();Toast.makeText(ShareActivity.this,"登录成功",Toast.LENGTH_LONG).show();}else {//2.2验证失败,提示用户Toast.makeText(ShareActivity.this,"账号密码错误",Toast.LENGTH_LONG).show();}}});}
}

2.activity_share代码

<?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=".ShareActivity"><TextViewandroid:id="@+id/textView7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginRight="8dp"android:layout_marginBottom="8dp"android:text="账号:"android:textSize="18sp"app:layout_constraintBottom_toTopOf="@+id/guideline3"app:layout_constraintEnd_toStartOf="@+id/guideline5" /><EditTextandroid:id="@+id/acc_edt"android:layout_width="350dp"android:layout_height="wrap_content"android:layout_marginBottom="8dp"app:layout_constraintBottom_toTopOf="@+id/guideline3"app:layout_constraintStart_toStartOf="@+id/guideline5"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginRight="8dp"android:layout_marginBottom="8dp"android:text="密码:"android:textSize="18sp"app:layout_constraintBottom_toTopOf="@+id/guideline4"app:layout_constraintEnd_toStartOf="@+id/guideline5" /><EditTextandroid:id="@+id/pwd_edt"android:layout_width="350dp"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:inputType="textPassword"app:layout_constraintBottom_toTopOf="@+id/guideline4"app:layout_constraintStart_toStartOf="@+id/guideline5" /><Buttonandroid:id="@+id/login_btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="登录"app:layout_constraintTop_toTopOf="@+id/guideline4"android:layout_margin="15dp" /><androidx.constraintlayout.widget.Guidelineandroid:id="@+id/guideline3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"app:layout_constraintGuide_begin="93dp" /><androidx.constraintlayout.widget.Guidelineandroid:id="@+id/guideline4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"app:layout_constraintGuide_begin="179dp" /><androidx.constraintlayout.widget.Guidelineandroid:id="@+id/guideline5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"app:layout_constraintGuide_begin="88dp" /></androidx.constraintlayout.widget.ConstraintLayout>

Android常用数据存储之SharedPreferences存储和读取用法分享相关推荐

  1. 数据存储方案(一) - 文件存储、SharedPreferences存储

    目录 文件存储 保存数据 保存输入框中的数据 读取数据 SharedPreferences存储 获取SharedPreferences对象 使用SharedPreferences保存数据 读取Shar ...

  2. 基于AndroidStudio的数据存储(SharedPreferences存储)的简单应用

    基于AndroidStudio的数据存储(SharedPreferences存储)的简单应用 前言 一.登录界面设计 二.MainActivity_10_5 中 前言 在手机的登录界面输入用户名和密码 ...

  3. Android 数据存储之SharedPreferences存储小记

    前言 Android的数据存储机制中还提供了SharedPreferences,SharedPreferences是这其中最容易理解的数据存储技术,采用键值对的方式进行存储,而且支持存储多中数据类型. ...

  4. Android入门(九)文件存储与SharedPreferences存储

    原文链接:http://www.orlion.ga/578/ Android系统中主要提供了三种方式用于简单地实现数据持久化功能,即文件存储.SharedPreference存储以及数据库存储.当然, ...

  5. AndroidStudio的数据存储(SharedPreferences存储)

    SharedPreferences存储 前言 一.SharedPreferences是什么? 二.使用步骤 (1)写入数据 (2)读出数据并使用Toast来展示 前言 本实验针对于AndroidStu ...

  6. android qq存储方式,android(5)(模拟QQ登录,文件存储,SD卡存储,SharedPreferences存储)...

    模拟QQ登录(记录账号和密码): 1.文件存储到文件中: /** * 文件存储 *@author Administrator * */ public class Utils { /** * 保存用户信 ...

  7. Android Studio数据永久保存——SharedPreferences

    数据永久保存--SharedPreferences 关于数据存储: 关于SharenPreferences 外部类访问SharedPreferences的数据的操作: 小结 本文为学习类文档,通过学习 ...

  8. Android 常用数据操作封装类案例

    1.DbHelper类 继承自SQLiteOpenHelper类,实现对数据库的基本操作 package com.example.utils;import android.content.Contex ...

  9. Android数据存储、文件存储、SQLite数据库简单使用、 sharedPreferences存储(五)

    文章目录 5 数据存储 5.1 数据的存储方式 5.2 文件存储 5.2.1 将数据存入文件中 5.2.2 从文件中读取数据 5.2.3 实战演练-保存QQ账号和密码(内部存储) 5.2.4 实战演练 ...

最新文章

  1. 面试中常问的List去重问题,你都答对了吗?
  2. 【Codeforces 339C】Xenia and Weights
  3. MySQL--用cmd命令查看已经建成的表
  4. matlab液压仿真模型,基于MATLABsimulink的液压系统动态仿真.ppt
  5. 【排队模型】基于粒子群优化核酸检测排队问题附matlab代码
  6. 条形码简介_条形码基本常识_条形码基本原理
  7. word复制某一页并插入到新页
  8. snapchat注册不到_如何将链接添加到您的Snapchat快照
  9. php 分段mp4合并,下载一个特殊的m3u8视频并合并为MP4
  10. IBM推出企业信息安全框架
  11. 微信小程序————简易计算器
  12. .net微信扫码支付
  13. 谷歌10年,难说再见......
  14. Oracle 压缩表占用空间、 UNDOTBS01.dbf 占用空间过大解决
  15. 孪生工厂:机械臂加工产线 HMI 监控界面
  16. java tcp dtu_【分享】使用有人DTU设备接入OneNet(基于TCP透传)
  17. c语言模拟器安卓版,C语言学习宝典电脑版下载
  18. 迅雷漫画下载工具II 故障日志09.04.12
  19. Newsstand报刊杂志
  20. 9、向导制作LQFP48L表贴封装

热门文章

  1. 算法小白如何高效、快速刷 LeetCode ?
  2. 【卡路里限制】多器官多类别单细胞分析思路2
  3. web界面——html基础
  4. 冯.诺伊曼体系结构的计算机工作原理是,冯诺依曼体系结构计算机的要点和工作过程.doc...
  5. 创建 Input Search 对象自己设计答题小程序 微信小程序设置自己的答题操作讲解 我要出题app|我要出题小程序 自定义微信答题小程序的制作方法 微信答题小程序 答题小程序
  6. Apifox接口自动化测试方法
  7. [转]MES与PCS系统的集成,实现了“双流”同步
  8. KANO模型(卡诺模型):产品人必懂的需求分析法
  9. AM335x SPL 代码分析
  10. 以智慧诠释城市新生活