概述: SharedPreferences 存储方式是采用键值对的方式存储数据的,存储和获取一条数据的时候需要通过相对应的键值来存储和获取。另外 SharedPreferences 是通过xml格式对数据进行管理。
想要通过 SharedPreferences 存储数据,就需要获取一个 SharedPreferences 对象。获取对象有三种方法。
-
Activity 类中的 getPreferences()方法。
-
Contex 类中的 getSharedPreferences()方法。
-
PreferenceManager 类中的 getDefaultSharedPreferences()方法。
今天讲解第三种方法,即 PreferenceManager 类中的getDefaultSharedPreferences()方法。
一.将数据存到SharedPreferences中。
步骤:
1.通过 SharedPreferences 类创建一个对象 preferences。
private SharedPreferences preferences;
2.通过 SharedPreferences.Editor 类创建对象 editor。
private SharedPreferences.Editor editor;
3.为对象 preferences 和对象 editor 赋予数据。
preferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = preferences.edit();
4.向对象editor中添加数据。
//从EditView中获取输入的字符串
String name = nameEditText.getText().toString();
String sex = sexEditText.getText().toString();
//保存到SharedPreferences中
editor.putString("name",name);
editor.putString("sex",sex);
//提交
editor.apply();
通过对象 editor 调用 putString(), putInt()... 等方法向editor中添加数据,根据你实际传入数据类型,这些方法名字以此类推。这些方法需要传入两个参数,第一个参数是键值,第二个参数是你的数据。获取 SharedPreferences 中数据也是通过键值来获取的。最后通过 editor.apply() 语句提交你传入的数据。这些数据就已经保存到 SharedPreferences 中了。
二:从SharedPreferences中读取数据。
从 SharedPreferences 中读取数据比较简单,只需要对象 preferences 调用 getString(), getInt()... 等方法就可以,根据键值对应的数据类型,方法名字一次类推。这些方法有两个参数,第一个参数是键值,和你存数据给的键值保持一致,第二个参数是默认值,即当你传入的键值没有相应的数据会以什么默认值进行返回。
保持上面的代码不变,再写入下面的代码即可获取 SharedPreferences 中的数据。我这里存放的都是字符串类型数据。
String name = preferences.getString("name","");
String sex = preferences.getString("sex","");
到这关于SharedPreferences存储就全部讲完了。
/******************************************************分割线**************************************************/
下面做一个小练习,在输入框中输入两个数据,然后将数据存入到SharedPreferences中,然后点击确定按钮,将数据显示在TextView中。
activity_main.xml 布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_height="60dp"
android:layout_width="match_parent">
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="姓名"
android:layout_gravity="center_vertical"
android:textSize="20sp"/>
<EditText
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="60dp"
android:layout_width="match_parent">
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="性别"
android:layout_gravity="center_vertical"
android:textSize="20sp"/>
<EditText
android:id="@+id/sex"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="显示内容:"/>
<Button
android:id="@+id/get_content"
android:layout_height="40dp"
android:layout_width="match_parent"
android:text="确定"
android:textSize="18sp"/>
</LinearLayout>
效果如图:
MainActivity.java 代码如下:
package com.example.shinelon.sharedpreferencesstorage;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private EditText nameEditText;
private EditText sexEditText;
private TextView contentTextView;
private Button getButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = (EditText) findViewById(R.id.name);
sexEditText = (EditText) findViewById(R.id.sex);
contentTextView = (TextView) findViewById(R.id.content);
getButton = (Button) findViewById(R.id.get_content);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = preferences.edit();
getButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//将数据采用SharedPreferences存储
save(editor);
//读取数据显示在TextView中
load(preferences);
}
});
}
//存入数据
private void save(SharedPreferences.Editor editor) {
String name = nameEditText.getText().toString();
String sex = sexEditText.getText().toString();
//存入数据
editor.putString("name",name);
editor.putString("sex",sex);
editor.apply();
}
//取数据
private void load(SharedPreferences preferences) {
String name = preferences.getString("name","");
String sex = preferences.getString("sex","");
//传入TextView
contentTextView.setText("姓名:" + name + "\n" + "性别:" + sex);
}
}
分别向输入框输入名字和性别,然后点击确认按钮,输入的数据就会显示到 TextView 中效果如图:
练习到这里就结束了。