安卓中sharepreference总结

来源:互联网 发布:淘宝修身包臀连衣裙 编辑:程序博客网 时间:2024/05/16 04:16

一、概述

        SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中。例如保存登录用户的用户名和密码。只能在同一个包内使用,不能在不同的包之间使用,其实也就是说只能在创建它的应用中使用,其他应用无法使用。

二、使用
       通过Context.getSharedPreferences方法获取SharedPreferences对象,参数分别为存储的文件名和存储模式。

三、操作

       SharePreferences存储数据是通过获取Editor编辑器对象来操作的。
调用Editor.putxxxx方法,两个参数分别为键和值。
调用Editor.getxxxx方法,两个参数分别为键和不存在指定键时的默认值。
调用Editor.remove方法,参数为指定的键。
调用Editor.clear方法
上述所有方法调用都要执行Editor.commit方法来提交。

四、直接上代码 ,

4.1 封装了sharepreferece 相关方法的类

package com.demo;import java.util.HashMap;import java.util.Map;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;public class PreferencesUtil {private Context context;public PreferencesUtil(Context context) {this.context = context;}/** * 保存参数 * @param name 姓名 * @param age 年龄 */public void save(String name, Integer age) {SharedPreferences preferences = context.getSharedPreferences("student", Context.MODE_PRIVATE);Editor editor = preferences.edit();editor.putString("name", name);editor.putInt("age", age);editor.commit();}/** * 获取各项配置参数 * @return */public Map<String, String> getPreferences(){Map<String, String> params = new HashMap<String, String>();SharedPreferences preferences = context.getSharedPreferences("student", Context.MODE_PRIVATE);params.put("name", preferences.getString("name", ""));params.put("age", String.valueOf(preferences.getInt("age", 0)));return params;}}
4.2 调用显示及其操作的Activity

package <span style="font-family: Arial;">com.demo.</span><span style="font-family: Arial;">settings;</span>import java.util.Map;import com.demo.PreferencesUtil;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {    private EditText nameText;    private EditText ageText;    private PreferencesService service;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        nameText = (EditText) this.findViewById(R.id.name);        ageText = (EditText) this.findViewById(R.id.age);        service = new PreferencesService(this);        Map<String, String> params = service.getPreferences();        nameText.setText(params.get("name"));        ageText.setText(params.get("age"));    }        public void save(View v){    String name = nameText.getText().toString();    String age = ageText.getText().toString();    service.save(name, Integer.valueOf(age));    Toast.makeText(getApplicationContext(), R.string.success, 1).show();    }        }
4.3 数据显示的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/name"    />        <EditText    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:id="@+id/name"    />        <TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/age"    />        <EditText    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:numeric="integer"    android:id="@+id/age"    />        <Button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/button"    android:onClick="save"    /></LinearLayout>
还有就是如果是新建的类,别忘了在AndroidManifest.xml中加上,今天关于安卓中偏好数据研究总结就到此

2 0