Android数据存储之SharePreferences参数应用
在Android中内存数据存储可以通过IO流读写数据
但是对于简单的数据存储(ex:用户信息、参数等)可以使用简单的SharePreferences参数来实现数据访问存储
1、通过SharePreferences参数保存数据文件位于内存的位置
/date/date/app包名/shared_prefs/文件名.xml
保存的内容为xml形式的键值对
2、应用SharePreferences参数的主要3个类
Ⅰ. android.content.Context /* 得到SharePreferences */
public abstract SharedPreferences getSharedPreferences(String name, int mode)
.....
Ⅱ. android.content.SharedPreferences /* 取数据 */
public abstract String getString(String key, String defValue)
......
Ⅲ. android.content.SharedPreferences.Editor /* Editor存数据 */
publicabstract SharedPreferences.Editor putString(String key, String value)
Android数据存储之内存读写SharePreferences应用实例
布局文件 activity_main.xml
<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_main_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="40dip"
android:textSize="20sp"
android:hint="请输入用户名"/>
<EditText
android:id="@+id/et_main_passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="40dip"
android:textSize="20sp"
android:hint="请输入密码"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="↑ SharedPreferences保存数据"
android:onClick="save"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="↓ SharedPreferences取出数据"
android:onClick="fetch"/>
<TextView
android:id="@+id/tv_main_spfinfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#98f5ff"
android:textSize="20sp"/>
</LinearLayout>
示例代码 MainActivity.java
package com.yihui.spf;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_main_username;
private EditText et_main_passwd;
private TextView tv_main_spfinfo;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_username = (EditText) findViewById(R.id.et_main_username);
et_main_passwd = (EditText) findViewById(R.id.et_main_passwd);
tv_main_spfinfo = (TextView) findViewById(R.id.tv_main_spfinfo);
sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE); //1.通过上下文得到SharedPreferences
//userinfo为文件名,后面是权限模式,即使文件名传null,则生成的文件名就是null.xml
}
/* 将数据保存到SharedPreferences参数 */
public void save(View v){
Editor editor = sp.edit(); //2.通过SharedPreferences得到编辑器用于保存数据
editor.putString("username", et_main_username.getText().toString()); //3.键值对的形式保存数据
editor.putString("passwd", et_main_passwd.getText().toString());
editor.commit(); //4.提交数据
Toast.makeText(MainActivity.this, "数据成功保存到SharedPreferences参数文件userinfo。xml", 0).show();
}
/* 从SharedPreferences参数取出数据 */
public void fetch(View v){
StringBuffer sb = new StringBuffer(); //5.取数据
sb.append("用户名: " + sp.getString("username", null)).append("\n密码: ").append(sp.getString("passwd", null));
tv_main_spfinfo.setText(sb);
}
}
运行效果 :
同时可以在/date/date/com.yihui.spf/shared_prefs/目录下看到已经生成了userinfo.xml内容
里面的内容就是UI文本框里的用户名和密码信息,且是键值对形式的xml文件,生成的userinfo.xml文件结果如下:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<map>
<string name="passwd">506269588</string>
<string name="username">liyihui</string>
</map>
SharePreferences主要用于保存app的一些配置信息
http://blog.csdn.net/yihuiworld