android数据存储之SharedPreferences
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="20sp"/>
<EditText
android:id="@+id/name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text=""
/>
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="20sp"/>
<EditText
android:id="@+id/age"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text=""
/>
</TableRow> <Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存"
android:textSize="20sp"
/>
<Button
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示"
android:textSize="20sp"
/>
<!-- 用于显示存储在SharedPreferences这种的数据 -->
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"/> </TableLayout>
(2)Activity中的MainActivity.java
package com.yby.sharedpreferences; import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private EditText name,age;
private Button save,show;
private TextView result; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//加载布局文件
setContentView(R.layout.main);
//初始化UI控件
name = (EditText) findViewById(R.id.name);
age = (EditText) findViewById(R.id.age);
save = (Button) findViewById(R.id.save);
show = (Button) findViewById(R.id.show);
result = (TextView) findViewById(R.id.result);
//为按钮监听事件
save.setOnClickListener(listener);
show.setOnClickListener(listener); } private OnClickListener listener = new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//获取SharedPreferences对象,最后会生成一个data.xml文件
/**
* SharedPreferences数据的四种操作模式
Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入
*/
SharedPreferences preferences = MainActivity.this.getSharedPreferences("data", Context.MODE_PRIVATE);
switch (v.getId()) {
case R.id.save:
//获取Editor对象
Editor editor = preferences.edit();
//Editor是一个map集合,通过putXXX()向里面塞值,key-value
editor.putString("name", name.getText().toString());
editor.putInt("age", Integer.parseInt(age.getText().toString()));
//此时必须要执行commit(),否则后面无法通过getXXX()来获取对应的值
editor.commit();
Toast.makeText(MainActivity.this,"保存成功" , Toast.LENGTH_SHORT).show();
break;
case R.id.show:
//getXXX(param1,param2) 第一个参数是存的时候的key,第二个是当在文件中找不到对应的key时,给的一个默认的值
String name = preferences.getString("name", "null");
int age = preferences.getInt("age", 0);
//由于我在上面没有给bir这个变量,所以这个会显示出来为bir is null
String bir = preferences.getString("bir", "bir is null");
//往TextView里面放存储在SharedPreferences中的值
result.setText("name:"+name+",age:"+age+",bir:"+bir);
break;
default:
break;
}
}
};
}
(3)运行项目实现效果

(4)通过SharedPreferences存储的数据保存在 /data/data/PACKAGE_NAME/shared_prefs目录下,可以通过eclipse中的DDMS导出XML文件查看,也可以用命令的方式进行查看
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">ffff</string>
<int name="age" value="12" />
</map>

通过命令的方式查看

android数据存储之SharedPreferences的更多相关文章
- Android数据存储-通过SharedPreferences实现记住密码的操作
在Android中登陆中,为了实现用户的方便,往往需要根据用户的需要进行记住密码的操作,所以,在Android数据存储中SharedPreferences恰恰可以实现这一点 下面,小编将带领大家通过S ...
- Android数据存储方式--SharedPreferences
Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...
- Android 数据存储之 SharedPreferences储存
------------------------------------------SharedPreferences存储--------------------------------------- ...
- Android数据存储三剑客——SharedPreferences、File、SQLite
Android中常用的数据存储一般有三种方式:SharedPreferences.文件和SQLite数据库,用来保存需要长时间保存的数据.本文将通过几个具体的小实例来讲解这三种方式的具体实现. 数据存 ...
- Android数据存储之SharedPreferences存储
安卓系统为应用提供了系统级的配置存储方案,它就是靠SharedPreferences接口来实现的,该接口存储的所有信息都是以名值对的形式保存,但其保存的数据类型也仅限于基本数据类型,如字符串.整形.布 ...
- Android数据存储之SharedPreferences使用
SharedPreferences是Android中一种轻型的数据存储类.本质上是基于XML文件进行存储Key-Value键值对的数据,生成的XML文件的目录在/data/data/包名/Shared ...
- Android数据存储之SharedPreferences及如何安全存储
前言: 最近一直在学习ios的数据存储,当学习到NSUserDefaults的时候让我回想起了SharedPreferences,今天闲来无事,想着总结一下SharedPreferences的使用. ...
- Android数据存储之sharedpreferences与Content Provider
android中对数据操作包含有: file, sqlite3, Preferences, ContectResolver与ContentProvider前三种数据操作方式都只是针对本应用内数据,程序 ...
- Android数据存储:Shared Preferences
Android数据存储之SharedPreferences 在Android系统中提供了多种存储技术.通过这些存储技术可以将数据存储在各种存储介质上, Android 为数据存储提供了如下几种方式:1 ...
随机推荐
- python学习札记(1)
首先给大家推荐一个很好的python入门网站,感觉比<python基础>之类的书更容易懂,廖雪峰小站,希望有学习资源同学们也能多多交流.下面是今天所学: 下面总结一些非常有特色的函数及其应 ...
- HDU 4004
http://acm.hdu.edu.cn/showproblem.php?pid=4004 题意:青蛙过长L的河,只能落在石头上,石头数量是n,给出n个坐标,至多跳m次,求在可以过河的条件下,青蛙跳 ...
- Selenium Waits
Selenium高级功能包含查找等待, Selenium的查找等待有两种方式, 隐式等待(Implicit Waits)和显示等待(Explicit Waits): 这里写下我对两者的理解, 1. 隐 ...
- SqlSever2005 一千万条以上记录分页数据库优化经验总结
http://www.cnblogs.com/jirigala/archive/2010/11/03/1868011.html 待测试???
- cmd命令进行RSA 密钥加密操作
--参考 http://msdn.microsoft.com/zh-cn/library/2w117ede http://msdn.microsoft.com/zh-cn/library/yxw286 ...
- 转:SQL SERVER数据库中实现快速的数据提取和数据分页
探讨如何在有着1000万条数据的MS SQL SERVER数据库中实现快速的数据提取和数据分页.以下代码说明了我们实例中数据库的“红头文件”一表的部分数据结构: CREATE TABLE [dbo]. ...
- 我的Java后端书架2016年暮春3.0版(转)
书架主要针对Java后端开发. 3.0版把一些后来买的.看的书添补进来,又或删掉或降级一些后来没有再翻开过的书. 更偏爱那些能用简短流畅的话,把少壮不努力的程序员所需的基础补回来的薄书,而有些教课书可 ...
- Apache搭建多个站点方法详解
www.111cn.net 编辑:Bolshevik 来源:转载 Apache的虚拟主机是一种允许在同一台机器上配置多个不同站点的web服务器环境的,就是iis一样可以创建多站点了,但是apache需 ...
- hdu 5902 Seam Carving
水题,直接上代码了 #include<cstdio> #include<cstring> #include<iostream> #include<cmath& ...
- 自动刷新ALV
转自http://blog.sina.com.cn/s/blog_701594f40100l8ms.html ABAP:利用SAP定时器自动刷新ALV 曾于无意之中发现,SAP系统中有个名为CL_GU ...