您的位置:首页 > Web前端

简单的数据存储--Preferences的使用

2013-05-27 14:52 267 查看
Preferences是一种应用程序内部轻量级的数据存储方案。Preferences主要用于存储和查询简单的数据类型的数据,包括:boolean、int、float、long以及String等,存储方式以键值对的形式存放在应用程序的私有文件夹下。

Preferences一般用来存储应用程序的设置信息,如应用程序的色彩,字体大小等,在应用程序中获取Preferences的方式如下

一、调用Context对象的getSharedPreferences方法获得SharedPreferences对象。需要传入SharedPreferences的名称和打开模式,名称为Preferences文件的名称,如果不存在则创建一个以传入名称为新的Preferences文件;打开模式为Private、MODE_WORLD_READABLE和MODE+WORLD_WRITEABLE其中之一。

二、调用Activity对象的getPreferences方法获得SharedPreferences对象。需要传入打开模式,打开模式为Private、MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE其中之一。

同样,通过两种不通途径获得的SharedPreferences对象并不是完全相同的,区别如下:

一、通过Context对象的getSharedPreferences方法获得的SharedPreferences对象可以被同意应用程序的其他组件共享

二、而使用Activity对象获得的SharedPreferences则只能在响应的Activity中使用

以下为简单的实现代码:

首先是需要配置布局文件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"
tools:context=".MainActivity" >

<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<EditText
android:id="@+id/myEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="none" />

<Button
android:id="@+id/myBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_button" />

</LinearLayout>


这里要实现的功能是,当每次启动Activity时,会从应用程序的Preferences文件中读取数据并显示出来;而每次退出Activity时,将EditText空间当前的内容存储到Preferences文件中,冲虚的Activity代码如下:

package com.demo.preferencespro;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

// 定义组件
private EditText myEdit = null;
private TextView myText = null;
private Button myBtn = null;
private SharedPreferences sharedPref = null;

public final String EDIT_TEXT_KEY = "EDIT_TEXT";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 取得组件
myEdit = (EditText) findViewById(R.id.myEdit);
myText = (TextView) findViewById(R.id.myText);
myBtn = (Button) findViewById(R.id.myBtn);

// 获得SharedPreference对象的引用
sharedPref = getPreferences(MODE_PRIVATE);

// 获取内容
String result = sharedPref.getString(EDIT_TEXT_KEY, null);

// 判断获取的值是否为空
if (result != null) {
myText.setText(result);
}

// 设置监听
myBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.img_logo)
.setMessage("退出本程序")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
MainActivity.this.finish();
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {

}
}).show();
}
});
}

@Override
protected void onDestroy() {
// 获得SharedPreferences的Editor对象
SharedPreferences.Editor editor = sharedPref.edit();
// 修改数据
editor.putString(EDIT_TEXT_KEY, String.valueOf(myEdit.getText()));
// 调用该方法以提交修改
editor.commit();
super.onDestroy();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


启动应用程序后,在EditText 控件中输入一些内容,然后关闭程序,当再次启动程序的时候TextView控件中讲显示上次退出时EditText里面的内容。

下图为程序第一次启动 并在EditText控件手动敲入文字后的效果



下图为第二次启动程序时屏幕的显示



下图为保存的Preferences文件内容。可以在data-->data-->com.demo.preferencespro(程序的包名称)-->shared_prefs中找到文件

文件路径:



文件内容,键值对。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: