Android之使用SharedPreferences保存用戶偏好參數


在Android應用中,我們常需要記錄用戶設置的一些偏好參數,,此時我們就需要用SharedPreferences和Editor將這些信息保存下來,在下次登錄時讀取。

SharedPreferences保存的數據主要類似於配置信息格式的數據,因此它保存數據的形式為key-value對,下面我們來看下實例代碼。

首先是界面布局,比較簡單,就是一個普通的登陸界面.

 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 <EditText 
11     android:layout_width="fill_parent"
12     android:layout_height="wrap_content"
13     android:id="@+id/account"
14     />
15 <EditText 
16     android:layout_width="fill_parent"
17     android:layout_height="wrap_content"
18     android:id="@+id/password"
19     android:layout_below="@id/account"
20     />
21 <Button 
22         android:layout_width="fill_parent"
23         android:layout_height="wrap_content"
24         android:layout_below="@id/password"
25         android:text="保存參數"
26         android:id="@+id/save"
27         android:onClick="save"
28  />
29 </RelativeLayout>

這是自定義的Preferences 類,用來實現數據的保存 ,可在Android的內置存儲空間產生一文件。

 1 import android.R.integer;
 2 import android.content.Context;
 3 import android.content.SharedPreferences;
 4 import android.content.SharedPreferences.Editor;
 5 import android.widget.EditText;
 6 
 7 public class Preferences {
 8 
 9     private Context context;
10     public Preferences(Context context)
11     {
12         this.context=context;
13     }
14     
15     
16     public void save(String name, Integer valueOf) 
17     {
18         //保存文件名字為"shared",保存形式為Context.MODE_PRIVATE即該數據只能被本應用讀取
19         SharedPreferences preferences=context.getSharedPreferences("shared",Context.MODE_PRIVATE);
20         
21         Editor editor=preferences.edit();
22         editor.putString("name", name);
23         editor.putInt("age", valueOf);
24         
25         editor.commit();//提交數據
26     }
27 
28 
29 }

 

下面是Mainactivity的代碼。在activity的oncreate階段我們加載本地的數據。

import java.util.HashMap;
import java.util.Map;

import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText account,passworad;
    Preferences prefer;//自定義的類
    SharedPreferences preference; 
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); account=(EditText)findViewById(R.id.account); passworad=(EditText)findViewById(R.id.password); //獲取本地的數據 preference=getSharedPreferences("shared", MODE_PRIVATE); Map<String, String> map=new HashMap<String, String>(); map.put("name",preference.getString("name","")); map.put("age", String.valueOf(preference.getInt("age", 0))); account.setText(map.get("name")); passworad.setText(map.get("age")); } //保存文件的方法 public void save(View v) { String name=account.getText().toString(); String age=passworad.getText().toString(); prefer=new Preferences(this); prefer.save(name,Integer.valueOf(age)); Toast.makeText(getApplicationContext(), "保存完成", Toast.LENGTH_SHORT).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }

我們看一下效果.

點擊保存參數,出現保存完成則說明我們已經保存成功了,在下次登錄的時候可以看到這些參數還在。因為記錄文件是在內置空間中的,所以我們在SD卡中找不到該文件,

如果有root權限的手機可以下載個RE文件管理,我們可以再/data/data/的路徑找到很多應用程序的內置文件夾,我們可以在這些文件夾中看到一個shared_prefs文件夾,

里面就有我們剛剛設置而產生的xml文件。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM