有些时候,应用程序有少量的数据需要保存,而且这些数据的格式很简单,都是普通的字符串. 标量类型的值等,Android提供了SharedPreferences进行保存。SharedPreferences处理的就是一个key-value(键值对)。从用法角度来看,SharedPreferences和SharedPreferences.Editor组合起来非常Map,其中SharedPreferences负责根据Key读取数据。而SharedPreferences.Editor则用于写入数据。
下面通过一个简单实例来说明:
1.XML
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <Button 12 android:id="@+id/button1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="保存数据到SharedPreferences" /> 16 17 <Button 18 android:id="@+id/button2" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="获取SharedPreferences中的数据" /> 22 23 </RelativeLayout>
2.java代码:
1 package com.example.sharedpreferences; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Context; 6 import android.content.SharedPreferences; 7 import android.view.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.Toast; 12 13 public class MainActivity extends Activity { 14 15 private Button button; 16 private Button button2; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 button = (Button) this.findViewById(R.id.button1); 23 button2 = (Button) this.findViewById(R.id.button2); 24 button.setOnClickListener(new OnClickListener() { 25 @Override 26 public void onClick(View arg0) { 27 saveSharPreference(MainActivity.this); 28 } 29 }); 30 button2.setOnClickListener(new OnClickListener() { 31 @Override 32 public void onClick(View arg0) { 33 getSharedPreference(); 34 } 35 }); 36 } 37 38 // 保存数据到sharedPreferennes 39 private void saveSharPreference(Context context) { 40 SharedPreferences sharedPreferences = getSharedPreferences("user", 41 Activity.MODE_PRIVATE); // 实例化SharedPreferences对象 42 SharedPreferences.Editor editor = sharedPreferences.edit(); // 实例化SharedPreferences.Editor对象 43 editor.putString("userid", "11"); 44 editor.putString("username", "summer"); 45 editor.commit(); 46 Toast.makeText(this, "数据成功写入SharedPreferences!", Toast.LENGTH_LONG) 47 .show(); 48 } 49 50 // 从SharedPreferences获取数据 51 private void getSharedPreference() { 52 SharedPreferences sharedPreferences = getSharedPreferences("user", 53 Activity.MODE_PRIVATE); 54 // 使用getString方法获得value,注意第2个参数是value的默认值 55 String userId = sharedPreferences.getString("userid", ""); 56 String userName = sharedPreferences.getString("username", ""); 57 Toast.makeText( 58 this, 59 "读取数据如下:" + " " + "userid:" + userId + " " + "username:" 60 + userName, Toast.LENGTH_LONG).show(); 61 } 62 }