见归档项目: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基础 分类: H1_ANDROID 2013-11-04 22:35 2559人阅读 评论(0) 收藏的更多相关文章

  1. iOS开发网络数据之AFNetworking使用 分类: ios技术 2015-04-03 16:35 105人阅读 评论(0) 收藏

    http网络库是集XML解析,Json解析,网络图片下载,plist解析,数据流请求操作,上传,下载,缓存等网络众多功能于一身的强大的类库.最新版本支持session,xctool单元测试.网络获取数 ...

  2. 全方位分析Objcetive-C Runtime 分类: ios技术 2015-03-11 22:29 77人阅读 评论(0) 收藏

    本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 简介 与Runtime交互 ...

  3. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  4. IOS之富文本编辑 分类: ios技术 2015-03-06 22:51 89人阅读 评论(0) 收藏

    之前做项目时遇到一个问题:          使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结 ...

  5. C/C++文字常量与常变量的概念与区别 分类: C/C++ 2015-06-10 22:56 111人阅读 评论(0) 收藏

    以下代码使用平台是Windows 64bits+VS2012. 在C/C++编程时,经常遇到以下几个概念:常量.文字常量.符号常量.字面常量.常变量.字符串常量和字符常量,网上博客资料也是千篇千律,不 ...

  6. 快速幂取模 分类: ACM TYPE 2014-08-29 22:01 95人阅读 评论(0) 收藏

    #include<stdio.h> #include<stdlib.h> //快速幂算法,数论二分 long long powermod(int a,int b, int c) ...

  7. Hibernate检索方式 分类: SSH框架 2015-07-10 22:10 4人阅读 评论(0) 收藏

    我们在项目应用中对数据进行最多的操作就是查询,数据的查询在所有ORM框架中也占有极其重要的地位.那么,如何利用Hibernate查询数据呢?Hibernate为我们提供了多种数据查询的方式,又称为Hi ...

  8. 【从0到1学Web前端】CSS伪类和伪元素 分类: HTML+CSS 2015-06-02 22:29 1065人阅读 评论(0) 收藏

    1.CSS中的伪类 CSS 伪类用于向某些选择器添加特殊的效果. 语法: selector : pseudo-class {property: value} CSS 类也可与伪类搭配使用 select ...

  9. CSS_Spirte实现原理 分类: HTML+CSS 2015-04-28 22:58 531人阅读 评论(0) 收藏

    CSS Spirte就是所谓的把很多的小图标合并成一张大的图片,然后使用CSS的background-position属性,来动态的定位自己需要图标的位置.这样做的目的主要是减少HTTP请求,加快网页 ...

随机推荐

  1. UVa 11094 - Continents

    题目:有一些岛屿在湖中.地图用两种字符表示.当前处在位置是一个岛屿.求除了当前岛屿外的最大岛屿. 分析:图论,floodfill.直接利用dfs求联通部分的面积就可以,然后取出最大. 说明:横线没有边 ...

  2. 68.qq号索引结构体写入内存,并实现快速排序

    //两个步骤,第一步读取文件,并且初始化索引结构体,把初始化的索引结构体写入到文件,第二步,读取这个文件到索引结构体 //并对这个结构体进行快速排序,得到顺序的索引,再写入文件 #define _CR ...

  3. JSTL之C标签学习

    JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.otherwise 3.循环标签:f ...

  4. Java中的线程模型及实现方式

    概念: 线程是一个程序内部的顺序控制流 线程和进程的比较: 每个进程都有独立的代码和数据空间(进程上下文),进程切换的开销大. 线程:轻量的进程,同一类线程共享代码和数据空间,每个线程有独立的运行栈和 ...

  5. libcurl 上传文件至 web服务器

    测试环境搭建, 使用 wamp server (windows下的 apache+MySQL+php) libcurl vc6 工程代码  下载地址:  http://download.csdn.ne ...

  6. 编译安装PHP-7.2.8

    一 下载并软件包 wget http://124.205.69.169/files/A218000006E9730A/cn2.php.net/distributions/php-7.2.8.tar.g ...

  7. Python画图参数设置

    https://blog.csdn.net/qiu931110/article/details/68130199

  8. vue实现一个会员卡的组件(可以动态传入图片(分出的一个组件)、背景、文字、卡号等)

    自己在写这个组件的时候主要遇到的问题就是在动态传入背景图片或者背景色的时候没能立马顺利写出来,不过现在实现了这个简单组件就和大家分享一下 <template> <div class= ...

  9. Java Web学习总结(11)——Session使用示例教程

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  10. (转)bat批处理的注释语句

    在批处理中,段注释有一种比较常用的方法: goto start = 可以是多行文本,可以是命令 = 可以包含重定向符号和其他特殊字符 = 只要不包含 :start 这一行,就都是注释 :start 另 ...