数据存储之SharedPreferences .
2011-07-25 12:23
453 查看
android中的数据存储方式多种,本地存储方式目前总结起来有:(1)通过file存储,即把数据保存在文件中。(2)SQLite存储,android上的一个微型数据库,虽小,但样样俱全。(3)SharedPreferences存储,其实我认为这种方式和第一种存储方式一样,只不过这种存储的数据是处理为xml数据存放在设备中。等等...
今天谈谈SharedPreferences的数据存储的实例实现。
假设有需求:一个应用程序需要对注册用户的姓名、年龄、性别进行存储,书写程序通过SharedPreferences完成实现。
好,先定义UI
![](http://hi.csdn.net/attachment/201011/9/3259801_12892785888Qvy.png)
如图,这个简单的demo操作流程:点击“保存”按钮,姓名、年龄、性别将被保存,点击“提取”,被保存的信息将会被提取并显示在按钮下方,在UI的实现上见xml
view plaincopy to clipboardprint?<?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>
代码不长,但实用性很高,呵呵,那么这里需要指出两点需要注意的地方,第一:创建SharedPreferences实例时,本段代码是通过 view plaincopy to clipboardprint?SharedPreferences sp = ActivityMain.this.getSharedPreferences("soft", Context.MODE_PRIVATE);
区别在哪?前一种创建时,会指定文件名soft,那后一种没有“soft”,就是没有文件名了?当然不是,这时,它会采用所在的类名作为默认文件名。 第二:SharedPreferences的实例不可以直接向文件中set或put数据,必须通过Editor才能完成数据录入工作,怪异的是,取数据时竟可直接通过SharedPreferences的实例进行get数据,同时,这里需要注意一个数据存储类型问题,这个demo中的存储数据类型都为String,所以put或get时它们的数据类型务必都是String。
今天谈谈SharedPreferences的数据存储的实例实现。
假设有需求:一个应用程序需要对注册用户的姓名、年龄、性别进行存储,书写程序通过SharedPreferences完成实现。
好,先定义UI
![](http://hi.csdn.net/attachment/201011/9/3259801_12892785888Qvy.png)
如图,这个简单的demo操作流程:点击“保存”按钮,姓名、年龄、性别将被保存,点击“提取”,被保存的信息将会被提取并显示在按钮下方,在UI的实现上见xml
view plaincopy to clipboardprint?<?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>
view plaincopy to clipboardprint?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); } }); } } 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() {@Overridepublic 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() {@Overridepublic 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实例时,本段代码是通过 view plaincopy to clipboardprint?SharedPreferences sp = ActivityMain.this.getSharedPreferences("soft", Context.MODE_PRIVATE);
view plaincopy to clipboardprint?SharedPreferences sp = ActivityMain.this.getPreferences(Context.MODE_PRIVATE); SharedPreferences sp = ActivityMain.this.getPreferences(Context.MODE_PRIVATE);
区别在哪?前一种创建时,会指定文件名soft,那后一种没有“soft”,就是没有文件名了?当然不是,这时,它会采用所在的类名作为默认文件名。 第二:SharedPreferences的实例不可以直接向文件中set或put数据,必须通过Editor才能完成数据录入工作,怪异的是,取数据时竟可直接通过SharedPreferences的实例进行get数据,同时,这里需要注意一个数据存储类型问题,这个demo中的存储数据类型都为String,所以put或get时它们的数据类型务必都是String。
![](http://hi.csdn.net/attachment/201011/9/3259801_1289293401wm4R.png)
相关文章推荐
- SharedPreferences 轻型的数据存储方式
- Android用SharedPreference存储数据
- android笔记20-数据存储之sharedPreference
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- Android数据存储之SharedPreference
- sharedPreference存储对象-数据库存储对象;android 将map保存到sqlite数据库中,从表数据中取回map
- Android学习13——数据存储之SharedPreference
- Android数据持久化之二:SharedPreferences 存储(下)
- Android的数据存储--SharedPreferences .
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- Android_数据存储——SharedPreferences .
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- Android的数据存储之SharedPreference与文件File
- Android 存储文件方式之一---SharedPreferences 内容提供者,以xml 的方式进行数据 存储。是一种轻量级的文件数据存储
- Android存储数据的方法:SharedPreference、File I/O、SQLite与ContentProvider分析
- Android数据存储之SharedPreferences 共享首选项/偏好设置
- Android的移动存储之SharedPreferences <可保存复杂数据>
- Android开发:SharedPreferences 存储数据、获取数据
- SharedPreferences ,用于存储、获取数据,实现登录功能
- android SharedPreferences 简单的数据存储