SharedPreferences基础
见归档项目: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基础的更多相关文章
- SharedPreferences基础 分类: H1_ANDROID 2013-11-04 22:35 2559人阅读 评论(0) 收藏
见归档项目:SharedPreferencesDemo.zip 1.对于数据量较小,且有明显的K-V形式的数据而言,适合用SharedPreferences保存.SharedPreferences的数 ...
- Android开发之数据存储——SharedPreferences基础知识详解,饿补学会基本知识,开发者必会它的用法。
一.数据存储选项:Data Storage --Storage Options[重点] 1.Shared Preferences Store private primitive data in key ...
- Android应用开发基础篇(9)-----SharedPreferences
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/27/2370319.html 一.概述 对于SharedPreferences,我吧它理解为一种 ...
- <Android基础> (六) 数据存储 Part 2 SharedPreferences方式
6.3 SharedPreferences存储 SharedPreferences使用键值对的方式来存储数据.同时支持多种不同的数据类型. 6.3.1 将数据存储到SharedPreferences中 ...
- android基础知识:SharedPreferences和PreferenceActivity
1.android文件存储 对Android系统了解的都知道,Android系统有四种基本的数据保存方法,一是SharedPreference,二是文件,三是SQLite,四是ContentProvi ...
- SharedPreferences具体解释(一)——基础知识
我们在开发软件的时候,常须要向用户提供软件參数设置功能,比如我们经常使用的微信,用户能够设置是否同意陌生人加入自己为好友.对于软件配置參数的保存,假设是在window下通常我们会採用ini文件进行保存 ...
- android基础---->SharedPreferences的使用
SharedPreferences 还支持多种不同的数据类型存储,如果存储的数据类型是整型,那么读取出来的数据也是整型的,存储的数据是一个字符串,读取出来的数据仍然是字符串.这样你应该就能明显地感觉到 ...
- Android SharedPreferences公共类sharedhelper
SimpAndroidFarme是近期脑子突然发热想做的android快速开发的框架,目标是模块化 常用的控件,方便新手学习和使用.也欢迎老鸟来一起充实项目:项目地址 sharedpreference ...
- Android数据存储之SharedPreferences及如何安全存储
前言: 最近一直在学习ios的数据存储,当学习到NSUserDefaults的时候让我回想起了SharedPreferences,今天闲来无事,想着总结一下SharedPreferences的使用. ...
随机推荐
- (转) Special members
原地址:http://www.cplusplus.com/doc/tutorial/classes2/ Special members [NOTE: This chapter requires p ...
- 有向强连通分支Tarjan算法
本文转载自:http://blog.csdn.net/xinghongduo/article/details/6195337 说到以Tarjan命名的算法,我们经常提到的有3个,其中就包括本文所介绍的 ...
- VS代码清理批处理
批处理清理VS工程 del /f /q /s *.ncb del /f /q /s *.sdf del /f /q /s /A H *.suo del /f /q /s *.ipch del /f / ...
- netCDF
NetCDF started in 1989 most used in geoscience community array-oriented self-describing header, desc ...
- centos 6.4从源码安装mysql 5.6笔记
上周在安装mysql时遇到了些许麻烦,今天整理下. 在代码目录建立obj文件夹. 在obj目录下,执行cmake .. -DXXX // XXX表示一些参数,详见http://dev.mysql.c ...
- php中12个魔术方法
本文列举了php面向对象当中12个魔术方法,并对此进行一一详细介绍,希望对新手有所帮助. 1.构造方法: __construct() 参数:自定义 触发时机:new的一瞬间自动调用 作用:初始化成员属 ...
- 使用ARM模板部署自动扩展的Linux VMSS(2)
12.准备完了模板文件,我们使用Powershell来创建VMSS for Linux的自动扩展集合,首先登陆到Azure中国的ARM账号: Login-AzureRmAccount -Environ ...
- HTML骨架-深入理解
HTML是WEB开发最基本的语言之一,也是最重要的语言之一,我们在浏览网页时做看到的内容是最直接的呈现形式就是HTML代码.<!DOCTYPE html PUBLIC "-//W3C/ ...
- thinkphp项目目录
# ThinkPHP核心文件介绍 ├─ThinkPHP.php 框架入口文件 ├─Common 框架公共文件 ├─Conf 框架配置文件 ├─Extend ...
- 简单了解下Dubbo
1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...