sharedPreferences存储数据

时间:2024-07-17 23:06:02

sharedPreferences使用的是键值对的方式存储数据。

1.Android中三种获取sharedPreferences的方式

1)Context 类中的getSharedPreferences()方法,该方法接受,第一个参数用于指定SharedPreferences 文件的名称,第二个参数用于指定操作模式,主要有两种模式可以选择,MODE_PRIVATE 和MODE_MULTI_PROCESS。

2)Activity 类中的getPreferences()方法,和上面的方法类似

3)PreferenceManager 类中的getDefaultSharedPreferences()方法 这是一个静态方法,它接收一个Context 参数。

获取sharedPreference之后,通过三步向其中添加数据

1.调用sharedPreference对象的edit()方法,获取SharedPreferences.Editor对象。

2.向Editor对象中添加数据。如,String类型数据用putString()方法。

3.通过调用commit()方法将添加的数据提交,从而完成数据存储操作。

获取sharedPreference中的数据的方法更加简单

1.获取sharedPreferens对象,

2.通过对象调用getString()方法,该方法传入两个参数,第一个参数是键值,第二个参数是该值得默认值。

现在代码实现:

界面直接写连个button,一个用于存储数据,一个用于取出数据。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical"
> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/save_data"
android:text="save data"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/get_date"
android:text="get date" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="2"
android:id="@+id/text"
android:hint="result is here"
/>
</LinearLayout>

xml

接着在.java文件中添加如下代码

 package com.example.yqt.sharedpreferencestest;

 import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button savadata;
Button getdata; TextView text; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); savadata = (Button)findViewById(R.id.save_data);
savadata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("name","Tom");
editor.putInt("age", 25);
editor.putBoolean("married", false);
editor.commit();
}
}); text = (TextView)findViewById(R.id.text); getdata = (Button)findViewById(R.id.get_date);
getdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
String name = pref.getString("name", "");
int age = pref.getInt("age", 0);
Boolean married = pref.getBoolean("married",false);
Log.d("MainActivity","name is "+name);
Log.d("MainActivity","age is "+age);
Log.d("MainActivity","married is "+married);
text.setText("name "+name+" age:"+age+" married:"+married);
}
}); } }

MainActivity.java

最后运行可以看到如下结果

sharedPreferences存储数据

这是保持后的数据导出的结果

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">Tom</string>
<boolean name="married" value="false" />
<int name="age" value="25" />
</map>

data