第59章、SharedPreferences存储(从零开始学Android)

转自:http://blog.csdn.net/jianghuiquan/article/details/8569226

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

  (1)获取SharedPreferences对象

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

  (3)通过Editor对象存储key-value键值对数据。

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

 

一、设计界面

  1、布局文件

  打开activity_main.xml文件。

  输入以下代码:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/save"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="保存数据" />  
  13.       
  14.     <Button  
  15.         android:id="@+id/read"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="读取数据" />  
  19.   
  20. </LinearLayout>  


二、程序文件

  打开“src/com.genwoxue.sharedpreferences/MainActivity.java”文件。

  然后输入以下代码:

[java] view plaincopy
  1. package com.genwoxue.sharedpreferences;  
  2.   
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7. import android.widget.Toast;  
  8. import android.app.Activity;  
  9. import android.content.SharedPreferences;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.     private Button btnSave=null;  
  14.     private Button btnRead=null;  
  15.       
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         btnSave=(Button)super.findViewById(R.id.save);  
  21.         btnRead=(Button)super.findViewById(R.id.read);  
  22.           
  23.         //保存sharedpreferences数据  
  24.         btnSave.setOnClickListener(new OnClickListener(){  
  25.             public void onClick(View v)  
  26.             {    
  27.                 //获取SharedPreferences对象  
  28.                 SharedPreferences share=MainActivity.this.getSharedPreferences("genwoxue", Activity.MODE_PRIVATE);  
  29.                 //使用Editor保存数据  
  30.                 SharedPreferences.Editor edit=share.edit();  
  31.                 edit.putString("url","www.genwoxue.com");  
  32.                 edit.putString("email""hello@genwoxue.com");  
  33.                 edit.commit();  
  34.                 Toast.makeText(getApplicationContext(), "保存成功!", Toast.LENGTH_LONG).show();  
  35.             }  
  36.         });  
  37.           
  38.         //读取sharedpreferences数据  
  39.         btnRead.setOnClickListener(new OnClickListener(){  
  40.             public void onClick(View v)  
  41.             {    
  42.                 //获取SharedPreferences  
  43.                 SharedPreferences share=MainActivity.this.getSharedPreferences("genwoxue", Activity.MODE_PRIVATE);  
  44.                 //使用SharedPreferences读取数据  
  45.                 String url=share.getString("url","");  
  46.                 String email=share.getString("email","");  
  47.                 //使用Toast显示数据  
  48.                 String info="跟我学编程网址:"+url+" 电子邮件:"+email;  
  49.                 Toast.makeText(getApplicationContext(), info, Toast.LENGTH_LONG).show();  
  50.             }  
  51.         });  
  52.     }  
  53. }  


三、运行结果  

   
原文地址:https://www.cnblogs.com/walccott/p/4957600.html