Android持久化技术之SharedPreferences存储

来源:互联网 发布:mysql base64 解码 编辑:程序博客网 时间:2024/05/13 03:15

       上一篇文章讲了文件存储,这篇文章准备讲一下SharedPreference存储。SharedPreference是通过键值对的方式存储数据的,使用SharedPreference来存储数据很简单,首先通过Context类中的getSharedPreferences()方法获取SharedPreferences对象,然后在通过SharedPreferences.edit()得到SharedPreferences.Editor对象,再向SharedPreferences.Editor中添加数据,最后再进行提交。下面举个例子吧。activity_main.xml中的代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="通过SharedPreference持久化数据"        android:textAllCaps="false"/></LinearLayout>

这里添加了个按钮,用于触发通过SharedPreferences保存数据事件


MainActivity中的代码如下:

package com.tangliang.shareepreferencepersisitence;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {    private Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();                editor.putString("name", "汤亮");                editor.putString("sex", "男");                editor.putInt("age", 21);                editor.putBoolean("hasGirlFriend", false);                editor.apply();            }        });    }}

从SharedPreferences中读取数据更简单,只需要通过SharedPreferences对象中的一系列的get方法就可以了,下面的例子在前面的例子中添加了一些东西,用于把保存的数据读取出来并在EditText中显示出来。

activity_main.xml中的代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="通过SharedPreference持久化数据"        android:textAllCaps="false"/>    <Button        android:id="@+id/restore_data"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="从SharedPreference恢复数据"        android:textAllCaps="false"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="name:"            android:textSize="25sp"/>        <EditText            android:id="@+id/name"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="sex:"            android:textSize="25sp"/>        <EditText            android:id="@+id/sex"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="age:"            android:textSize="25sp"/>        <EditText            android:id="@+id/age"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="hasGirlFriend:"            android:textSize="25sp"/>        <EditText            android:id="@+id/hasGirlFriend"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"/>    </LinearLayout></LinearLayout>

MainActivity中的代码如下:

package com.tangliang.shareepreferencepersisitence;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends AppCompatActivity {    private Button button;    private Button restoreButton;    private EditText nameText;    private EditText sexText;    private EditText ageText;    private EditText hasGirlFriendText;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);        restoreButton = (Button) findViewById(R.id.restore_data);        nameText = (EditText) findViewById(R.id.name);        sexText = (EditText) findViewById(R.id.sex);        ageText = (EditText) findViewById(R.id.age);        hasGirlFriendText = (EditText) findViewById(R.id.hasGirlFriend);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();                editor.putString("name", "汤亮");                editor.putString("sex", "男");                editor.putInt("age", 21);                editor.putBoolean("hasGirlFriend", false);                editor.apply();            }        });        restoreButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);                String name = pref.getString("name", "");                String sex = pref.getString("sex", "");                int age = pref.getInt("age", 0);                boolean hasGirlFriend = pref.getBoolean("hasGirlFriend", false);                nameText.setText(name);                sexText.setText(sex);                ageText.setText("" + age);                hasGirlFriendText.setText(" " + hasGirlFriend);            }        });    }}


原创粉丝点击