標籤:android style blog http color ar os 使用 sp
有些時候,應用程式有少量的資料需要儲存,而且這些資料的格式很簡單,都是普通的字串. 標量類型的值等,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 <Button12 android:id="@+id/button1"13 android:layout_width="wrap_content"14 android:layout_height="wrap_content"15 android:text="儲存資料到SharedPreferences" />16 17 <Button18 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 @Override19 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 @Override26 public void onClick(View arg0) {27 saveSharPreference(MainActivity.this);28 }29 });30 button2.setOnClickListener(new OnClickListener() {31 @Override32 public void onClick(View arg0) {33 getSharedPreference();34 }35 });36 }37 38 // 儲存資料到sharedPreferennes39 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("test",53 Activity.MODE_PRIVATE);54 // 使用getString方法獲得value,注意第2個參數是value的預設值55 String name = sharedPreferences.getString("userid", "");56 String habit = sharedPreferences.getString("username", "");57 Toast.makeText(this,58 "讀取資料如下:" + "\n" + "name:" + name + "\n" + "habit:" + habit,59 Toast.LENGTH_LONG).show();60 }61 }
Android資料存放區之SharedPreferences