Android資料存放區之SharedPreferences

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.