使用SharedPrenference 存储数据类似ios 的偏好设置存储数据
适用范围:保存少量的数据,且这些数据的格式非常简单:字符串类型,基本类型的值。比如应用程序的各种配置信息(如是否打开音效、是否使用震动效果、小游戏的玩家积分等),解锁口令密码等。
核心原理:保存基于XML文件存储的key-value键值对数据,通常用来存储一些简单的配置信息。通过DDMS 的file explorer面板,展开文件浏览树,很明显sharepreferences数据总是存储在/data/data/<package name>/shared_prefs目录下,sharedpreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过sharepreferences.edit(),sharepreferences本身是一个接口,程序无法直接创建sharedpreference实例,只能通过contenxt提供的getsharedprenferences(string name,int mode)方法来获取sharedprenferences实例,该方法中的name表示要操作的xml文件名,Editor有如下主要重要方法:
SharedPreferences.Editor clear():清空SharedPreferences里所有数据
SharedPreferences.Editor putXxx(String key , xxx value):向SharedPreferences存入指定key对应的数据,其中xxx可以是boolean,float,int等各种基本类型据
SharedPreferences.Editor remove():删除SharedPreferences中指定key对应的数据项
boolean commit(): 当Editor编辑完成后,使用该方法提交修改
示例:直接上代码哈,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="com.data.cn.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入密码口令" />
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="设置解锁口令"
/>
<Button
android:id="@+id/btnget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取解锁口令"
/>
</LinearLayout>
mainactivity中代码:
package com.data.cn;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {
private Button setBtn;
private Button getBtn;
private EditText edText;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getBaseContext();
setBtn = (Button) findViewById(R.id.btnset);
getBtn = (Button) findViewById(R.id.btnget);
edText = (EditText) findViewById(R.id.et);
setBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String password = edText.getText().toString().trim();
SharedPreferences.Editor editor = getSharedPreferences("lock", context.MODE_PRIVATE).edit();
editor.putString("password", password);
editor.commit();
}
});
getBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences read = getSharedPreferences("lock", context.MODE_PRIVATE);
String password = read.getString("password", "");
Toast.makeText(context, password, 12000).show();
}
});
}
}