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的使用. ...
随机推荐
- CDZSC_2015寒假新人(2)——数学 P
P - P Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- webService设置超时时间
在客户端配置文件中设置: <bindings> <basicHttpBinding> <binding name="UrlCrawler ...
- mac终端 使用摘要
Root (拥有对此计算机的所有权限) 查看当前用户:who 切换到root(默认系统是不会使用root的):sudo -s 然后输入密码 更改密码:passwd
- Eclipse使用git最简易流程
git有诸多好处,网上都说的很清楚了,在这里我不再赘述.对于我来说,私下里想做一些项目,而又不能很好的保存自己的代码和进行版本控制,这时候,就用到了git.下面,就以我个人为例讲讲git从0开始如何安 ...
- 微软的OneDrive研究~
Dropbox 很好,唯一觉得不爽的是只能同步指定的目录.不过被墙之后就不那么方便了,所以改用微软的 Live Mesh,缺点是支持的设备少(仅 PC 和 Mac). https://technet. ...
- SQL Serverf 索引 - 索引压缩 、附加特性 <第十篇>
一.索引压缩 数据和索引压缩在SQL Server2008被引入.压缩一个索引意味着将在一个页面中获得更多的关键字信息.这可以造成重大的性能改进,因为存储索引需要的页面和索引级别更少.因为索引中的键值 ...
- 【HDU 2586 How far away?】LCA问题 Tarjan算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...
- 图片旋转+剪裁js插件(兼容各浏览器) « 张鑫旭-鑫空间-鑫生活
图片旋转+剪裁js插件(兼容各浏览器) « 张鑫旭-鑫空间-鑫生活 图片旋转+剪裁js插件(兼容各浏览器) by zhangxinxu from http://www.zhangxinxu.com 本 ...
- iOS 无效的版本,提交成功,不出现版本号
最近更新到 iOS 10,提交审核 会卡在 转菊花 ...需要更新到Xcode 8 去提交. 然后提交成功后,版本管理 新版本,构建版本 迟迟不出来.恭喜你,你的版本是无效的.请看看 你的 公司app ...
- HDU 1695 GCD 欧拉函数+容斥定理
输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...