Android持久化技术之SharedPreferences存储实例详解
作者:残缺的孤独 发布时间:2023-03-25 03:17:10
标签:Android,持久化,SharedPreferences
本文实例讲述了Android持久化技术之SharedPreferences存储。分享给大家供大家参考,具体如下:
1、SharedPreferences存储
在前面一篇文章《Android持久化技术之文件的读取与写入实例详解》中,我们介绍了Android持久化技术的文件的读取与写入。在本文中,继续介绍Android持久化技术另外一个SharedPreferences存储。
(1)SharedPreferences存储方式是基于key-value的,通过key可以找到对应的value。
(2)支持多种数据类型存储,比如字符串、整形、布尔型等,并有对应的存储与获取方法。
(3)获取SharedPreferences对象有多种方式。
使用Context类的getSharedPreferences方法。
使用Activity类的getPreferences方法
使用PreferenceManager类的getDefaultSharedPreferences方法
(4)当存储时,需要通过SharedPreferences对象获取SharedPreferences.Editor对象
(5)默认存储路径为:/data/data/包名/shared_prefs/目录
(6)存储文件类型为xml文件
2、示例
场景:点击保存按钮,存储数据;点击恢复按钮,恢复数据。
(1)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account:" />
<EditText
android:id="@+id/account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Input your account here"
android:ems="10" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
>
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/login"
android:layout_span="2"
android:layout_height="wrap_content"
android:text="save data" />
</TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:background="#ff0000"
android:text="我是万恶的分割线"
android:textSize="20sp"
android:gravity="center"
/>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account:" />
<EditText
android:id="@+id/account2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:id="@+id/password2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
>
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/login2"
android:layout_span="2"
android:layout_height="wrap_content"
android:text="restore data" />
</TableRow>
</TableLayout>
(2)MainActivity.java
package com.example.testsharedpreferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Android 持久化技术-----SharedPreferences存储
* @author yy
*
*/
public class MainActivity extends Activity {
private EditText accountEdit;
private EditText passwordEdit;
private Button saveButton;
private Button restoreButton;
private SharedPreferences pref;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//存储按钮
saveButton = (Button) findViewById(R.id.login);
//为存储按钮添加点击事件
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//获取SharedPreferences对象
//第一个参数:文件名,没有则新建。第二个参数:写入模式-覆盖
pref = getSharedPreferences("second", MODE_PRIVATE);
//获取SharedPreferences.Editor对象
editor = pref.edit();
//获取输入的账号内容
accountEdit = (EditText) findViewById(R.id.account);
String account = accountEdit.getText().toString();
//获取输入的密码内容
passwordEdit = (EditText) findViewById(R.id.password);
String password = passwordEdit.getText().toString();
//存储用户名和密码
editor.putString("account", account);
editor.putString("password", password);
//提交
editor.commit();
Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
}
});
//获取恢复按钮对象
restoreButton = (Button) findViewById(R.id.login2);
//添加事件
restoreButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//获取SharedPreference对象
pref = getSharedPreferences("second", MODE_PRIVATE);
//读取内容
String account = pref.getString("account", "this is default value");
String password = pref.getString("password", "this is default value");
//设置到响应位置
EditText editText2 = (EditText)findViewById(R.id.account2);
editText2.setText(account);
EditText passwordText2 = (EditText) findViewById(R.id.password2);
passwordText2.setText(password);
Toast.makeText(getApplicationContext(), "恢复成功", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3、结果
输入内容后,当点击“save data”按钮后,存储文件为second.xml,如下:
对应内容:
下面是效果图:
希望本文所述对大家Android程序设计有所帮助。


猜你喜欢
- 定义栈又名堆栈,是一种操作受限的线性表,仅能在表尾进行插入和删除操作。它的特点是先进后出,就好比我们往桶里面放盘子,放的时候都是从下往上一个
- 需求在配置类中,从application.properties中读取一个复杂list。如List<Person>或者初始化一个m
- 1、创建控制台程序如上图所示,选择linux开发平台,我用的VS2019,.Net5.0,一直点下一步,创建。2、创建TCP服务端程序usi
- 一、音乐播放器的实现原理 Javase的多媒体功能很弱,所以有一个专门处理多媒体的插件叫JMF,JMF提供的模型可大致分为七类*
- 本文实例讲述了android获取屏幕高度和宽度的实现方法。分享给大家供大家参考。具体分析如下:我们需要获取Android手机或Pad的屏幕的
- 单一职责原则:一个类,只有一个引起它变化的原因。为什么需要单一职责原则?如果一个类有多个原因要去修改它,那么修改一个功能时,可能会让其他功能
- 将SpringBoot项目部署到腾讯云注意:1、如果已经下载好MySql和JDK,可以直接跳过1、3步骤。但是不要忘记步骤2哦。2、如果已经
- 从不规则的字符串中截取出日期最近在项目中需要远程调接口,从String字符串中截取出日期,想了好久,最后用java8新特性,解决了,java
- 前言ConcurrentHashMap是Java 5中支持高并发、高吞吐量的线程安全HashMap实现。我们知道,ConcurrentHas
- multipartResolver上传文件配置1、gradle配置 compile ('commons-i
- 采集器概貌,如下:最近做一个项目,功能类似于CNZZ站长统计功能,要求显示Ip所在的省份市区/提供商等信息。网上的Ip纯真数据库,下载下来一
- 本篇超级详细案例截图教学 IDEA如何运行SpringBoot项目,图片点击可放大仔细看Java编译工具以及环境准备:IDEA JDK1.8
- 一,下载Zookeeper,地址为http://archive.apache.org/dist/zookeeper/,找到你要下载的版本,我
- C#中Directory.GetFiles() 函数的使用C#中Directory.GetFiles(string path , strin
- 今天看了看Java并发程序,写一写入门程序,并设置了线程的优先级。class Elem implements Runnable{  
- 有时候,根据业务逻辑的需求,我们想要获取到某个接口的所有实现类。在这里大致介绍两种方式:1.借助Spring容器实现Spring作为一个容器
- Spring定时任务无故停止又不报错一开始是使用Spring自带的定时器来配置定时任务的,简单快捷,配置如下:<bean id=&qu
- 最近搞造价系统时遇到一些需要汇总的指标数据类似下面的结构指标A 1000指标B 500指标C 500指标A = B+C当我们需要对这些数值进
- 一、前言代码死循环这个话题,个人觉得还是挺有趣的。因为只要是开发人员,必定会踩过这个坑。如果真的没踩过,只能说明你代码写少了,或者是真正的大
- 现在视频应用越来越火,Periscope火起来后,国内也出现了不少跟风者,界面几乎跟Periscope一模一样.Periscope确实不错,