標籤:
SharedPreferences類 供開發人員儲存和擷取基礎資料型別 (Elementary Data Type)的索引值對。
該類主要用於基本類型,例如:booleans,ints,longs,strings。在應用程式結束後,資料仍舊會儲存。
有兩種方式可以獲得SharedPreferences對象
1、getSharedPreferences(): 如果需要多個使用名稱來區分的共用檔案,則可以使用該方法,其第一個參數就是共用檔案的名稱。
對於使用同一個名稱獲得的多個SharedPreferences引用,其指向同一個對象
2、getPreferences(): 如果activity僅需要一個共用檔案,則可以使用該方法。因為只有一個共用檔案,它並不需要提供名稱。
向SharedPreferences類中增加值的方法如下
1、調用SharedPreferences類的edit()方法獲得SharedPreferences對象
2、調用諸如putString(),putInt()等方法增加相應類型的值
3、使用commit()方法提交新的值
從SharedPreferences類中讀取值時,主要使用該類中定義的getXXX()方法。
下面用一個簡單的例子來練習SharedPreferences類的使用
首先看布局檔案:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textView1" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:layout_alignParentLeft="true"12 android:layout_alignParentTop="true"13 android:layout_marginLeft="66dp"14 android:layout_marginTop="64dp"15 android:text="使用者名稱:" />16 17 <TextView18 android:id="@+id/textView2"19 android:layout_width="wrap_content"20 android:layout_height="wrap_content"21 android:layout_alignLeft="@+id/textView1"22 android:layout_below="@+id/textView1"23 android:layout_marginTop="32dp"24 android:text="密碼:" />25 26 <EditText27 android:id="@+id/editText1"28 android:layout_width="wrap_content"29 android:layout_height="wrap_content"30 android:layout_alignBaseline="@+id/textView1"31 android:layout_alignBottom="@+id/textView1"32 android:layout_toRightOf="@+id/textView1"33 android:ems="10" />34 35 <EditText36 android:id="@+id/editText2"37 android:layout_width="wrap_content"38 android:layout_height="wrap_content"39 android:layout_alignBaseline="@+id/textView2"40 android:layout_alignBottom="@+id/textView2"41 android:layout_alignLeft="@+id/editText1"42 android:ems="10"43 android:inputType="textPassword" >44 45 <requestFocus />46 </EditText>47 48 <Button49 android:id="@+id/btn_load"50 android:layout_width="wrap_content"51 android:layout_height="wrap_content"52 android:layout_alignLeft="@+id/editText2"53 android:layout_below="@+id/editText2"54 android:layout_marginTop="29dp"55 android:text="登入" />56 57 </RelativeLayout>
View Code
一個簡單的登入介面。
再看JAVA檔案
1 package data; 2 3 import com.example.allcode.R; 4 5 import android.app.Activity; 6 import android.content.SharedPreferences; 7 import android.content.SharedPreferences.Editor; 8 import android.os.Bundle; 9 import android.view.View;10 import android.view.View.OnClickListener;11 import android.widget.Button;12 import android.widget.EditText;13 import android.widget.Toast;14 15 public class Sharedpreference_use extends Activity{16 private EditText name;17 private EditText password;18 private Button load;19 20 @Override21 protected void onCreate(Bundle savedInstanceState) {22 // TODO Auto-generated method stub23 super.onCreate(savedInstanceState);24 setContentView(R.layout.data_sharedpreferences);25 26 name = (EditText) findViewById(R.id.editText1);27 password = (EditText) findViewById(R.id.editText2);28 load = (Button) findViewById(R.id.btn_load);29 30 //登入按鈕,將使用者名稱和密碼存到SharedPreferences對象中儲存資料31 //通過toast顯示儲存的使用者名稱和密碼32 load.setOnClickListener(new OnClickListener() {33 34 @Override35 public void onClick(View v) {36 // TODO Auto-generated method stub37 String str_name = name.getText().toString(); //擷取使用者名稱編輯框的資料38 String str_password = password.getText().toString(); //擷取密碼框中的資料39 40 //獲得私人類型的SharedPreferences41 42 SharedPreferences sp = getSharedPreferences("mrsoft", MODE_PRIVATE);43 Editor editor = sp.edit(); //擷取Editor對象44 editor.putString("username", str_name); //添加使用者名稱45 editor.putString("uesrpassword", str_password); //添加密碼46 editor.commit(); //提交資料47 48 //如果在另一個activity中擷取SharedPreferences儲存的資料 ,要加上下面這行代碼,在同一activity種則不需要49 //SharedPreferences sp = getSharedPreferences("mrsoft", MODE_PRIVATE);50 String get_name = sp.getString("username","0");51 String get_password = sp.getString("uesrpassword","1");52 53 Toast.makeText(Sharedpreference_use.this, "通過SharedPreferences儲存的使用者名稱為:"+get_name+"密碼為:"+get_password, 1).show();54 //將擷取的使用者名稱和密碼的資料通過toast顯示出來55 }56 });57 58 59 60 }61 62 }
:
安卓開發_資料存放區技術_SharedPreferences類