歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之使用SharedPreferences保存用戶偏好參數

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

日期:2017/3/1 9:16:02   编辑:Linux編程

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

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

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/account"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
android:layout_below="@id/account"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/password"
android:text="保存參數"
android:id="@+id/save"
android:onClick="save"
/>
</RelativeLayout>

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

import android.R.integer;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.EditText;

public class Preferences {

private Context context;
public Preferences(Context context)
{
this.context=context;
}


public void save(String name, Integer valueOf)
{
//保存文件名字為"shared",保存形式為Context.MODE_PRIVATE即該數據只能被本應用讀取
SharedPreferences preferences=context.getSharedPreferences("shared",Context.MODE_PRIVATE);

Editor editor=preferences.edit();
editor.putString("name", name);
editor.putInt("age", valueOf);

editor.commit();//提交數據
}


}

下面是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文件。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved