数据存储之SharedPreferences .

 

android中的数据存储方式多种,本地存储方式目前总结起来有:(1)通过file存储,即把数据保存在文件中。(2)SQLite存储,android上的一个微型数据库,虽小,但样样俱全。(3)SharedPreferences存储,其实我认为这种方式和第一种存储方式一样,只不过这种存储的数据是处理为xml数据存放在设备中。等等...

今天谈谈SharedPreferences的数据存储的实例实现。

假设有需求:一个应用程序需要对注册用户的姓名、年龄、性别进行存储,书写程序通过SharedPreferences完成实现。

好,先定义UI

如图,这个简单的demo操作流程:点击“保存”按钮,姓名、年龄、性别将被保存,点击“提取”,被保存的信息将会被提取并显示在按钮下方,在UI的实现上见xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <RelativeLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content">  
  10.       
  11.         <TextView   
  12.             android:layout_width="wrap_content"   
  13.             android:layout_height="wrap_content"   
  14.             android:text="@string/name"  
  15.             android:textSize="30px"  
  16.             android:id="@+id/tv_name"  
  17.             />  
  18.         <EditText  
  19.             android:layout_width="200px"   
  20.             android:layout_height="wrap_content"   
  21.             android:layout_toRightOf="@id/tv_name"  
  22.             android:layout_alignTop="@id/tv_name"  
  23.             android:layout_marginLeft="10px"  
  24.             android:id="@+id/et_name"  
  25.             />  
  26.     </RelativeLayout>  
  27.       
  28.     <RelativeLayout  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content">  
  31.         <TextView    
  32.             android:layout_width="wrap_content"   
  33.             android:layout_height="wrap_content"   
  34.             android:text="@string/age"  
  35.             android:textSize="30px"  
  36.             android:id="@+id/tv_age"  
  37.             />  
  38.         <EditText  
  39.             android:layout_width="200px"   
  40.             android:layout_height="wrap_content"   
  41.             android:layout_toRightOf="@id/tv_age"  
  42.             android:layout_alignTop="@id/tv_age"  
  43.             android:layout_marginLeft="10px"  
  44.             android:inputType="number"  
  45.             android:id="@+id/et_age"  
  46.             />  
  47.     </RelativeLayout>      
  48.       
  49.     <RelativeLayout  
  50.         android:layout_width="fill_parent"  
  51.         android:layout_height="wrap_content">  
  52.         <TextView    
  53.             android:layout_width="wrap_content"   
  54.             android:layout_height="wrap_content"   
  55.             android:text="@string/sex"  
  56.             android:textSize="30px"  
  57.             android:id="@+id/tv_sex"  
  58.             />  
  59.         <EditText  
  60.             android:layout_width="200px"   
  61.             android:layout_height="wrap_content"   
  62.             android:layout_toRightOf="@id/tv_sex"  
  63.             android:layout_alignTop="@id/tv_sex"  
  64.             android:layout_marginLeft="10px"  
  65.             android:id="@+id/et_sex"  
  66.             />  
  67.     </RelativeLayout>    
  68.   
  69.     <RelativeLayout  
  70.         android:layout_width="fill_parent"  
  71.         android:layout_height="wrap_content">  
  72.           
  73.         <Button  
  74.             android:layout_width="wrap_content"  
  75.             android:layout_height="wrap_content"  
  76.             android:text="@string/save"  
  77.             android:id="@+id/btn_sava"  
  78.             />  
  79.         <Button  
  80.             android:layout_width="wrap_content"  
  81.             android:layout_height="wrap_content"  
  82.             android:layout_toRightOf="@id/btn_sava"  
  83.             android:layout_alignTop="@id/btn_sava"  
  84.             android:text="@string/take"  
  85.             android:id="@+id/btn_take"  
  86.             />  
  87.     </RelativeLayout>  
  88.       
  89.     <TextView  
  90.         android:layout_width="fill_parent"  
  91.         android:layout_height="wrap_content"  
  92.         android:id="@+id/tv_result"  
  93.         />  
  94.           
  95. </LinearLayout>  

很简单的实现,线性布局嵌套关系布局,考虑到本地化的问题,当然少不了string.xml文件,这个会在这个demo程序包中,

那么主要的就是看看这个activity如何参考实现,

  1. package com.app;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.SharedPreferences;  
  6. import android.content.SharedPreferences.Editor;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. public class ActivityMain extends Activity {  
  16.       
  17.     private EditText etName;  
  18.     private EditText etAge;  
  19.     private EditText etSex;  
  20.       
  21.     private Button btnSave;  
  22.     private Button btnTake;  
  23.       
  24.     private TextView tvResult;  
  25.       
  26.     /** Called when the activity is first created. */  
  27.     @Override  
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.main);  
  31.           
  32.         etName = (EditText)this.findViewById(R.id.et_name);  
  33.         etAge = (EditText)this.findViewById(R.id.et_age);  
  34.         etSex = (EditText)this.findViewById(R.id.et_sex);  
  35.           
  36.         btnSave = (Button)this.findViewById(R.id.btn_sava);  
  37.         btnTake = (Button)this.findViewById(R.id.btn_take);  
  38.         tvResult = (TextView)this.findViewById(R.id.tv_result);  
  39.           
  40.         btnSave.setOnClickListener(new OnClickListener() {  
  41.             @Override  
  42.             public void onClick(View v) {  
  43.                 String strName = etName.getText().toString();  
  44.                 String strAge = etAge.getText().toString();  
  45.                 String strSex = etSex.getText().toString();  
  46.                   
  47.                 //创建SharedPreferences实例   
  48.                 SharedPreferences sp =  ActivityMain.this.getSharedPreferences("soft", Context.MODE_PRIVATE);  
  49.                 //创建编辑器editor,通过编辑器把数据存放到sp中   
  50.                 Editor editor = sp.edit();  
  51.                 editor.putString("name", strName);  
  52.                 editor.putString("age", strAge);  
  53.                 editor.putString("sex", strSex);  
  54.                 editor.commit();//提交变更   
  55.                 Toast.makeText(ActivityMain.this, R.string.toast_saveSuccee, 1).show();  
  56.             }  
  57.         });  
  58.           
  59.         btnTake.setOnClickListener(new OnClickListener() {  
  60.             @Override  
  61.             public void onClick(View v) {  
  62.             //创建Sharedpreferences实例   
  63.                 SharedPreferences sp = ActivityMain.this.getSharedPreferences("soft", Context.MODE_PRIVATE);  
  64.                 //提取数据时可以直接通过sp提取数据,这一点和添加数据时不同   
  65.                 String name = sp.getString("name""");  
  66.                 String age = sp.getString("age""");  
  67.                 String sex = sp.getString("sex""");  
  68.                   
  69.                 tvResult.setText("姓名:" + name +"/n年龄:" + age + "/n性别:" + sex);  
  70.             }  
  71.         });  
  72.           
  73.     }  
  74. }  

代码不长,但实用性很高,呵呵,那么这里需要指出两点需要注意的地方,第一:创建SharedPreferences实例时,本段代码是通过

  1. SharedPreferences sp = ActivityMain.this.getSharedPreferences("soft", Context.MODE_PRIVATE);  
这种方法实现的,那还有另一种方法,即
  1. SharedPreferences sp = ActivityMain.this.getPreferences(Context.MODE_PRIVATE);  

区别在哪?前一种创建时,会指定文件名soft,那后一种没有“soft”,就是没有文件名了?当然不是,这时,它会采用所在的类名作为默认文件名。 第二:SharedPreferences的实例不可以直接向文件中set或put数据,必须通过Editor才能完成数据录入工作,怪异的是,取数据时竟可直接通过SharedPreferences的实例进行get数据,同时,这里需要注意一个数据存储类型问题,这个demo中的存储数据类型都为String,所以put或get时它们的数据类型务必都是String。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值