Android数据存储之SharedPreferences

来源:互联网 发布:淘宝热卖紫砂壶推荐 编辑:程序博客网 时间:2024/05/19 03:28

SharedPreferences是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。其存储位置在/data/data/<包名>/shared_prefs目录下。SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。实现SharedPreferences存储的步骤如下:

  一、根据Context获取SharedPreferences对象

  二、利用edit()方法获取Editor对象。

  三、通过Editor对象存储key-value键值对数据。

  四、通过commit()方法提交数据。

下面根据一个小例子来更好的理解SharedPreferences的用法:当按返回键退出程序时,利用SharedPreferences实现对用户名和密码的存储,当再次打开程序就会保存有用户名和密码。

阶段一:进行界面布局(如下图)

                  

具体代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:padding="@dimen/padding_medium"            android:text="@string/username"            tools:context=".MainActivity" />        <EditText            android:id="@+id/name"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:inputType="text" />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:padding="@dimen/padding_medium"            android:text="@string/password"            tools:context=".MainActivity" />        <EditText            android:id="@+id/password"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:inputType="textPassword" />    </LinearLayout></LinearLayout>

阶段二:编写MainActivity,查找组件并进行相应的事件处理。具体代码如下:

package com.lks.sharedpreference;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.Menu;import android.view.MenuItem;import android.widget.EditText;import android.support.v4.app.NavUtils;public class MainActivity extends Activity {public static final String SETTING_INFOS="SETTING_infos"; private EditText nameText;private EditText passwordText;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        SharedPreferences sharedPreferences=getSharedPreferences(SETTING_INFOS, MODE_PRIVATE);        nameText=(EditText) this.findViewById(R.id.name);        passwordText=(EditText) this.findViewById(R.id.password);        String name=sharedPreferences.getString("name", "");        String password=sharedPreferences.getString("password", "");        if(name!=null&&!"".equals(name)){            nameText.setText(name);         }        if(password!=null&&!"".equals(password)){              passwordText.setText(name);        }    }        @Overrideprotected void onStop() {super.onStop();String name=nameText.getText().toString();String password=passwordText.getText().toString();SharedPreferences sharedPreferences=getSharedPreferences(SETTING_INFOS, Context.MODE_PRIVATE);Editor editor=sharedPreferences.edit();editor.putString("name", name);editor.putString("password", password);editor.commit();}@Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }    }

阶段三:输入用户名和密码

                              

然后按返回键退出程序,当再次打开程序,会出现已经保存的用户名和密码,如下图:

                              

注:SharedPreferences是以XML的格式以文件的方式自动保存的,在DDMS中的File Explorer中展开到 /data/data/<package name>/shared_prefs下。


 

 

原创粉丝点击