您的位置:首页 > Web前端

数据存储之SharedPreferences(系统偏好设置)(一)

2016-04-21 17:30 417 查看
SharedPreferences使用Map数据结构来存储数据,以键值对的方式存数,采用了XML格式将数据存储到设备中。一般来讲,使用SharedPreferences主要用来存储一些用户喜好的设置信息,比如是否愿意接收推送通知、是否愿意自动同步等。

下面以一个实例来演示如何使用SharedPreferences来完成数据的存储。

(1)布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用户名" />
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="年龄" />
<EditText
android:id="@+id/age"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer" />
<Button
android:id="@+id/saveBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存个人信息" />
<Button
android:id="@+id/readBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="读取个人信息" />

</LinearLayout>


(2)编辑MainActivity.java文件

package com.example.chapter8_1;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
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;

public class MainActivity extends Activity implements OnClickListener {
/** 存储后的文件路径:/data/data/<package name>/shares_prefs + 文件名.xml */
private  static final String FILE_NAME = "info";/** 存储的文件名 */

EditText usernameEdit;//声明用户名输入框控件
EditText ageEdit;       //声明年龄输入框控件
Button saveBtn;     //声明保存个人信息按钮
Button readBtn;    //声明读取个人信息按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEdit = (EditText)findViewById(R.id.username);//找到id为username的输入框控件
ageEdit = (EditText)findViewById(R.id.age);//找到id为age的输入框控件
saveBtn = (Button)findViewById(R.id.saveBtn);//找到id为saveBtn的按钮控件
readBtn = (Button)findViewById(R.id.readBtn);//找到id为readBtn的按钮控件
saveBtn.setOnClickListener(this);//绑定单击事件监听器
readBtn.setOnClickListener(this);//绑定单击事件监听器
}
@Override
public void onClick(View v) {
if(R.id.saveBtn == v.getId()){//保存个人信息
saveInfo();
}else if(R.id.readBtn == v.getId()){//读取个人信息
readInfo();
}
}

//保存个人信息
private void saveInfo(){
//获取SharedPreferences对象
SharedPreferences infoPref = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
//获取Editor对象
Editor editor = infoPref.edit();
String username = usernameEdit.getText().toString();//获取用户名
int  age = Integer.parseInt(ageEdit.getText().toString());//获取年龄
editor.putString("username", username);//存放字符串数据
editor.putInt("age", age);                      //存放整型数据
editor.commit();                                        //保存
Toast.makeText(this, "保存个人信息成功", Toast.LENGTH_LONG).show();
}

//读取个人信息
private void readInfo(){
//获取SharedPreferences对象
SharedPreferences infoPref = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
String username = infoPref.getString("username", "");//获取key为“username”所对应的值
int age = infoPref.getInt("age", 0);//获取key为"age"所对应的值
Toast.makeText(this, "用户名:" + username + " 年龄:" + age, Toast.LENGTH_LONG).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}


说明:getSharedPreferences(String name, int mode)方法的第一个参数用于指定该文件的名称,名称不用带后缀,后缀会由Android自动加上。第二个参数指定文件的操作模式,共有4种操作模式:

Context.MODE_PRIVATE: 默认操作模式,指定该SharedPreferences数据只能被本应用程序读、写。

Context.MODE_WORLD_READABLE: 指定该SharedPreferences数据能被其他应用程序读,但不能写。

Context.MODE_WORLD_WRITEABLE: 指定该SharedPreferences数据能被其他应用程序写入。

Context.MODE_APPEND:该模式会检查文件是否存在,存在就往指定的文件中追加内容,否则就创建新的文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据存储 Preference