android中的数据存储方式多种,本地存储方式目前总结起来有:(1)通过file存储,即把数据保存在文件中。(2)SQLite存储,android上的一个微型数据库,虽小,但样样俱全。(3)SharedPreferences存储,其实我认为这种方式和第一种存储方式一样,只不过这种存储的数据是处理为xml数据存放在设备中。等等...
今天谈谈SharedPreferences的数据存储的实例实现。
假设有需求:一个应用程序需要对注册用户的姓名、年龄、性别进行存储,书写程序通过SharedPreferences完成实现。
好,先定义UI
如图,这个简单的demo操作流程:点击“保存”按钮,姓名、年龄、性别将被保存,点击“提取”,被保存的信息将会被提取并显示在按钮下方,在UI的实现上见xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/name"
- android:textSize="30px"
- android:id="@+id/tv_name"
- />
- <EditText
- android:layout_width="200px"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/tv_name"
- android:layout_alignTop="@id/tv_name"
- android:layout_marginLeft="10px"
- android:id="@+id/et_name"
- />
- </RelativeLayout>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/age"
- android:textSize="30px"
- android:id="@+id/tv_age"
- />
- <EditText
- android:layout_width="200px"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/tv_age"
- android:layout_alignTop="@id/tv_age"
- android:layout_marginLeft="10px"
- android:inputType="number"
- android:id="@+id/et_age"
- />
- </RelativeLayout>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/sex"
- android:textSize="30px"
- android:id="@+id/tv_sex"
- />
- <EditText
- android:layout_width="200px"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/tv_sex"
- android:layout_alignTop="@id/tv_sex"
- android:layout_marginLeft="10px"
- android:id="@+id/et_sex"
- />
- </RelativeLayout>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/save"
- android:id="@+id/btn_sava"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/btn_sava"
- android:layout_alignTop="@id/btn_sava"
- android:text="@string/take"
- android:id="@+id/btn_take"
- />
- </RelativeLayout>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/tv_result"
- />
- </LinearLayout>
很简单的实现,线性布局嵌套关系布局,考虑到本地化的问题,当然少不了string.xml文件,这个会在这个demo程序包中,
那么主要的就是看看这个activity如何参考实现,
- package com.app;
- 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.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class ActivityMain extends Activity {
- private EditText etName;
- private EditText etAge;
- private EditText etSex;
- private Button btnSave;
- private Button btnTake;
- private TextView tvResult;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- etName = (EditText)this.findViewById(R.id.et_name);
- etAge = (EditText)this.findViewById(R.id.et_age);
- etSex = (EditText)this.findViewById(R.id.et_sex);
- btnSave = (Button)this.findViewById(R.id.btn_sava);
- btnTake = (Button)this.findViewById(R.id.btn_take);
- tvResult = (TextView)this.findViewById(R.id.tv_result);
- btnSave.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String strName = etName.getText().toString();
- String strAge = etAge.getText().toString();
- String strSex = etSex.getText().toString();
- //创建SharedPreferences实例
- SharedPreferences sp = ActivityMain.this.getSharedPreferences("soft", Context.MODE_PRIVATE);
- //创建编辑器editor,通过编辑器把数据存放到sp中
- Editor editor = sp.edit();
- editor.putString("name", strName);
- editor.putString("age", strAge);
- editor.putString("sex", strSex);
- editor.commit();//提交变更
- Toast.makeText(ActivityMain.this, R.string.toast_saveSuccee, 1).show();
- }
- });
- btnTake.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- //创建Sharedpreferences实例
- SharedPreferences sp = ActivityMain.this.getSharedPreferences("soft", Context.MODE_PRIVATE);
- //提取数据时可以直接通过sp提取数据,这一点和添加数据时不同
- String name = sp.getString("name", "");
- String age = sp.getString("age", "");
- String sex = sp.getString("sex", "");
- tvResult.setText("姓名:" + name +"/n年龄:" + age + "/n性别:" + sex);
- }
- });
- }
- }
代码不长,但实用性很高,呵呵,那么这里需要指出两点需要注意的地方,第一:创建SharedPreferences实例时,本段代码是通过
- SharedPreferences sp = ActivityMain.this.getSharedPreferences("soft", Context.MODE_PRIVATE);
- SharedPreferences sp = ActivityMain.this.getPreferences(Context.MODE_PRIVATE);
区别在哪?前一种创建时,会指定文件名soft,那后一种没有“soft”,就是没有文件名了?当然不是,这时,它会采用所在的类名作为默认文件名。 第二:SharedPreferences的实例不可以直接向文件中set或put数据,必须通过Editor才能完成数据录入工作,怪异的是,取数据时竟可直接通过SharedPreferences的实例进行get数据,同时,这里需要注意一个数据存储类型问题,这个demo中的存储数据类型都为String,所以put或get时它们的数据类型务必都是String。