见归档项目:SharedPreferencesDemo.zip

1、对于数据量较小,且有明显的K-V形式的数据而言,适合用SharedPreferences保存。SharedPreferences的数据以xml文件的形式保存在/data/data/包名/SharedPreferences的目录下,如下例:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="hi3">liaoliuqing</string>
<string name="hi">liaoliuqing</string>
<string name="name">liaoliuqing</string>
</map>

2、写入SharedPreferences数据的基本步骤

(1)获取SharedPreferences实例

(2)通过SharedPreferences的实例获取Editor实例。注:SharedPreferences本身并没有提供写入数据的能力,而是通过其内部类SharedPreferences.Editor来实现。

(3)调用Editor的write方法,切记后面还要commit。

3、读取SharedPreferences数据的基本步骤

(1)获取SharedPreferences实例

(2)读取单一数据:调用SharedPreferences的getXxx()方法,分别读取String,int,long等类型。

(3)读取全量数据:调用SharedPreferences的getAll()方法,再遍历map。

实例:

图形界面如下

<LinearLayout 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:orientation="vertical"
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:id="@+id/et_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/key" /> <EditText
android:id="@+id/et_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/value" /> <Button
android:id="@+id/bt_write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/write" /> <Button
android:id="@+id/bt_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read" /> <TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </LinearLayout>
package com.ljh.sharepreferencedemo;

import java.util.Map;
import java.util.Map.Entry; import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final EditText etKey = (EditText) findViewById(R.id.et_key);
final EditText etValue = (EditText) findViewById(R.id.et_value);
Button btRead = (Button) findViewById(R.id.bt_read);
Button btWrite = (Button) findViewById(R.id.bt_write);
final TextView tvContent = (TextView) findViewById(R.id.tv_content); //1、获取SharedPreferences对象。SharedPreferences是一个接口,程序无法直接创建SharedPreferences对象,只能通过Context提供的getSharedPreferences()方法。
final SharedPreferences preference = getSharedPreferences("test", MODE_PRIVATE); //2、获取SharedPreferences。Editor对象,用于写数据。本步骤只对写数据有必要。
final SharedPreferences.Editor editor = preference.edit(); btWrite.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
String key = etKey.getText().toString().trim();
String value = etValue.getText().toString().trim();
//3、写入数据并commit。
editor.putString(key, value);
editor.commit();
} }); btRead.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
//2、根据key值读取单一数据
/* String content = preference.getString("name", "lujinhong");
tvContent.setText(content);*/
//2、读取所有数据
String content = "";
Map<String, ?> allContent = preference.getAll();
//注意遍历map的方法
for(Map.Entry<String, ?> entry : allContent.entrySet()){
content+=(entry.getKey()+entry.getValue());
}
tvContent.setText(content); } }); } }

SharedPreferences基础的更多相关文章

  1. SharedPreferences基础 分类: H1_ANDROID 2013-11-04 22:35 2559人阅读 评论(0) 收藏

    见归档项目:SharedPreferencesDemo.zip 1.对于数据量较小,且有明显的K-V形式的数据而言,适合用SharedPreferences保存.SharedPreferences的数 ...

  2. Android开发之数据存储——SharedPreferences基础知识详解,饿补学会基本知识,开发者必会它的用法。

    一.数据存储选项:Data Storage --Storage Options[重点] 1.Shared Preferences Store private primitive data in key ...

  3. Android应用开发基础篇(9)-----SharedPreferences

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/27/2370319.html 一.概述 对于SharedPreferences,我吧它理解为一种 ...

  4. <Android基础> (六) 数据存储 Part 2 SharedPreferences方式

    6.3 SharedPreferences存储 SharedPreferences使用键值对的方式来存储数据.同时支持多种不同的数据类型. 6.3.1 将数据存储到SharedPreferences中 ...

  5. android基础知识:SharedPreferences和PreferenceActivity

    1.android文件存储 对Android系统了解的都知道,Android系统有四种基本的数据保存方法,一是SharedPreference,二是文件,三是SQLite,四是ContentProvi ...

  6. SharedPreferences具体解释(一)——基础知识

    我们在开发软件的时候,常须要向用户提供软件參数设置功能,比如我们经常使用的微信,用户能够设置是否同意陌生人加入自己为好友.对于软件配置參数的保存,假设是在window下通常我们会採用ini文件进行保存 ...

  7. android基础---->SharedPreferences的使用

    SharedPreferences 还支持多种不同的数据类型存储,如果存储的数据类型是整型,那么读取出来的数据也是整型的,存储的数据是一个字符串,读取出来的数据仍然是字符串.这样你应该就能明显地感觉到 ...

  8. Android SharedPreferences公共类sharedhelper

    SimpAndroidFarme是近期脑子突然发热想做的android快速开发的框架,目标是模块化 常用的控件,方便新手学习和使用.也欢迎老鸟来一起充实项目:项目地址 sharedpreference ...

  9. Android数据存储之SharedPreferences及如何安全存储

    前言: 最近一直在学习ios的数据存储,当学习到NSUserDefaults的时候让我回想起了SharedPreferences,今天闲来无事,想着总结一下SharedPreferences的使用. ...

随机推荐

  1. 已知TSP问题的最好解

    a280 : 2579ali535 : 202339att48 : 10628att532 : 27686bayg29 : 1610bays29 : 2020berlin52 : 7542bier12 ...

  2. codeforces 22C System Administrator(构造水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud System Administrator Bob got a job as a s ...

  3. 图的割点 桥 双连通(byvoid)

    [点连通度与边连通度] 在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合.一个图的点连通度的定义为,最小割点集 ...

  4. JuPyter(IPython) Notebook中通过pip安装第三方Python Module

    JuPyter(IPython) Notebooks中使用pip安装Python的模块 刚开始接触JuPyter Notebook的时候觉得这是个不错的写技术博客的工具,可以很直观的把代码和结果结合在 ...

  5. js判断数组和对象

    <script> var arr=new Array(); var obj={'1':2}; var num=11; function isType(obj){ if(obj instan ...

  6. php常用的header头

    <?php /** * php常用的header头设置... */ header('HTTP/1.1 200 OK'); // ok 正常访问 header('HTTP/1.1 404 Not ...

  7. MVC中一般为什么用IQueryable而不是用IList?用IQueryable比IList好在哪?

    IList(IList<T>)会立即在内存里创建持久数据,这就没有实现"延期执行(deferred execution)",如果被加载的实体有关联实体(associat ...

  8. SQL Server索引设计 <第五篇>

    SQL Server索引的设计主要考虑因素如下: 检查WHERE条件和连接条件列: 使用窄索引: 检查列的选择性: 检查列的数据类型: 考虑列顺序: 考虑索引类型(聚集索引OR非聚集索引): 一.检查 ...

  9. SQL Server 2008空间数据应用系列六:基于SQLCRL的空间数据可编程性

    原文:SQL Server 2008空间数据应用系列六:基于SQLCRL的空间数据可编程性 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 ...

  10. SQL Server 2008空间数据应用系列四:基础空间对象与函数应用

    原文:SQL Server 2008空间数据应用系列四:基础空间对象与函数应用 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 R2调测. ...