Android中的数据存储(一):SharedPreferences 2017-05-24 10:35 64人阅读 评论(1) 收藏
令密码等。
Explorer面板,展开文件浏览树,很明显SharedPreferences数据总是存储在/data/data/<package name>/shared_prefs目录下。
个接口,程序无法直接创建SharedPreferences实例,只能通过Context提供的getSharedPreferences(String name, int mode)方法来获取SharedPreferences实例。该方法中name表示要操作的xml文件名,第二个参数具体如下:
Context.MODE_PRIVATE: 指定该SharedPreferences数据只能被本应用程序读、写。
Context.MODE_WORLD_READABLE: 指定该SharedPreferences数据能被其他应用程序读,但不能写。
Context.MODE_WORLD_WRITEABLE: 指定该SharedPreferences数据能被其他应用程序读,写
clear():清空SharedPreferences里所有数据
SharedPreferences.Editor putXxx(String key , xxx value): 向SharedPreferences存入指定key对应的数据,其中xxx 可以是boolean,float,int等各种基本类型据
SharedPreferences.Editor remove(): 删除SharedPreferences中指定key对应的数据项
boolean commit(): 当Editor编辑完成后,使用该方法提交修改
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.star.sharedpreferences.activity.MainActivity"
android:orientation="vertical"
android:gravity="center"
android:background="#d7d5d5"> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:orientation="horizontal"> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="保存"
android:background="#fff"
android:textSize="20dp"/> <EditText
android:id="@+id/save"
android:layout_marginLeft="20dp"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#fff"
/>
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_marginTop="20dp"> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="获取"
android:textSize="20dp"
android:background="#fff"
android:id="@+id/textView" /> <EditText
android:id="@+id/get"
android:layout_marginLeft="20dp"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#fff"/>
</LinearLayout> <Button
android:id="@+id/bt_save"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:background="#fff"/> <Button
android:id="@+id/bt_get"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取"
android:background="#fff"/> </LinearLayout>
ui是不是很简单,初学者都能轻易的看懂,好了完成基本的ui布局后,我们再来看avtivity里:activity里的初始化控件直接用的butterknife,可以少些很多代码,让代码更简练。下面代码中红色部分是存储数据的方法(此处要注意:在editor编辑之后一定要调用commit方法提交一下,否则操作无效果),蓝色部分是获取数据的方法。
package com.example.star.sharedpreferences.activity; import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; import com.example.star.sharedpreferences.R;
import com.example.star.sharedpreferences.utils.MyToast; import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @BindView(R.id.save)
EditText mEditTextSave;
@BindView(R.id.get)
EditText mEditTextGet;
@BindView(R.id.bt_save)
Button mButtonSave;
@BindView(R.id.bt_get)
Button mButtonGet; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this); } @OnClick({R.id.bt_save, R.id.bt_get})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.bt_save:
String data = mEditTextSave.getText().toString().trim();
if (TextUtils.isEmpty(data)){
MyToast.show(getApplicationContext(),"不能为空");
return;
}
SharedPreferences.Editor editor = getSharedPreferences("text",MODE_PRIVATE).edit();
editor.putString("data",data);
editor.commit();
MyToast.show(getApplicationContext(),"数据保存成功");
break;
case R.id.bt_get:
SharedPreferences sp = getSharedPreferences("text",MODE_PRIVATE);
String dataGet = sp.getString("data","");
mEditTextGet.setText(dataGet);
MyToast.show(getApplicationContext(),"数据提取成功");
break;
}
}
}
Android中的数据存储(一):SharedPreferences 2017-05-24 10:35 64人阅读 评论(1) 收藏的更多相关文章
- SharedPreferences基础 分类: H1_ANDROID 2013-11-04 22:35 2559人阅读 评论(0) 收藏
见归档项目:SharedPreferencesDemo.zip 1.对于数据量较小,且有明显的K-V形式的数据而言,适合用SharedPreferences保存.SharedPreferences的数 ...
- ubuntu中安装eclipse 分类: android ubuntu linux 学习笔记 2015-07-07 10:19 75人阅读 评论(0) 收藏
上一篇说了安装jdk的事,于是趁热打铁,决定把eclipse也安装了. 下载这一系列就不用说了. 下载完成之后: 然后解压,解压之后文件剪切到/usr/software文件夹中,同时重命名为eclip ...
- C/C++中const的用法 分类: C/C++ 2015-07-05 00:43 85人阅读 评论(0) 收藏
const是C语言的关键字,经C++进行扩充,变得功能强大,用法复杂.const用于定义一个常变量(只读变量),当const与指针,引用,函数等结合起来使用时,情况会变得复杂的多.下面将从五个方面总结 ...
- 解析ASP.NET Mvc开发之查询数据实例 分类: ASP.NET 2014-01-02 01:27 5788人阅读 评论(3) 收藏
目录: 1)从明源动力到创新工场这一路走来 2)解析ASP.NET WebForm和Mvc开发的区别 ----------------------------------------------- ...
- 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏
也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...
- C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏
1. 概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...
- iOS开发网络数据之AFNetworking使用 分类: ios技术 2015-04-03 16:35 105人阅读 评论(0) 收藏
http网络库是集XML解析,Json解析,网络图片下载,plist解析,数据流请求操作,上传,下载,缓存等网络众多功能于一身的强大的类库.最新版本支持session,xctool单元测试.网络获取数 ...
- 【从0到1学Web前端】javascript中的ajax对象(一) 分类: JavaScript 2015-06-24 10:18 754人阅读 评论(1) 收藏
现在最流行的获取后端的(浏览器从服务器)数据的方式就是通过Ajax了吧.今天就来详细的来学习下这个知识吧.如果使用ajax来访问后段的数据,浏览器和浏览器端的js做了那些工作呢?我做了一个图,请大家看 ...
- Java获取项目中的路径 分类: Java Game 2014-08-14 10:17 122人阅读 评论(0) 收藏
在项目中经常需要获取某个文件的路径: 在这里提供一些获取路径的方法.. 1.此种方式获取的路径,是当前类所在的路径: UserDAOTest.class.getResource("UserD ...
随机推荐
- APP版本号记录
VoLTE版本: VT_BV0800V1.0.0B06 800M版本: NETARTIST_BV0800V1.0.0B01 看详细版本号:9831275#
- photoshop 安装问题
问题:“安装程序检测到计算机重新启动操作可能处于挂起状态.建议您退出安装程序,重新启动并重试.” 解决: 1.运行 regedit 打开注册表编辑器. 2.依次展开HKEY_LOCAL_MACHINE ...
- C基础 如何让代码只执行一次
1.0 最简单, 最高效的方式 C 代码运行起点 main 就是个大单例函数. 如果把函数注册在其里面, 那么一定很可以 :) // 某个库需要初始化的函数 void log_init(void) { ...
- python 内置函数eval()、exec()、compile()
eval 函数的作用: 计算指定表达式的值.也就是说它要执行的python代码只能是单个表达式,而不是复杂的代码逻辑. eval(source, globals=None, locals=Non ...
- Java-悲观锁和乐观锁
Java中的乐观锁与悲观锁: 1. Java中典型的synchronized就是一种悲观锁,也就是独占锁,不过JDK1.6之后对synchronized已经做了许多优化,也不能说是完全的悲观锁了: 2 ...
- cgic实现输入文件名,打开文件的功能
a.c文件 #include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg. ...
- mktime(将时间结构数据转换成经过的秒数)
mktime(将时间结构数据转换成经过的秒数)表头文件#include<time.h>定义函数time_t mktime(strcut tm * timeptr);函数说明mktime() ...
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
- 强大到无与伦比的Graphviz
图1 hello world 尝试画复杂一些的图: 一直苦苦寻找用于图论的画图软件,偶然在Matrix67的这篇博文里找到. Graphviz使用dot语言,这门不仅语言非常简单易学,而且功能却非常强 ...