Android SharedPreferences(数据存储,需掌握)
1.SharePrefences类介绍

SharedPreferences 类提供了一个通用框架,以便您能够保存和检索原始数据类型的永久性键值对。 您可以使用 SharedPreferences 来保存任何原始数据:布尔值、浮点值、整型值、长整型和字符串。 此数据将跨多个用户会话永久保留(即使您的应用已终止亦如此)。
文件存储位置://文件存放地址 //data/data/包名/shared_pres

2.使用方法



3.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入存储的数据:" /> <EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="存储" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取" />
</LinearLayout> <TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
4.java后台
package com.lucky.test45sharedpreferences; import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { EditText editText1;
TextView textView2;
Button button1;
Button button2;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1=findViewById(R.id.editText);
textView2=findViewById(R.id.textView2);
button1=findViewById(R.id.button);
button2=findViewById(R.id.button2); //1.获取SharedPreferences实例,去保存数据,参数1为生成的xml文件的名称,参数2是文件的模式
sharedPreferences=getSharedPreferences("lucky",MODE_WORLD_WRITEABLE); //初始化SharedPreferences
editor=sharedPreferences.edit(); //2.获取编辑器
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.putString("001",editText1.getText().toString()); //3.放入所需存储的数据
editor.commit(); //4.提交数据
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=sharedPreferences.getString("001",""); //读取数据,参数1为数据代号,参数2为默认值
textView2.setText(str);
}
});
}
}
5.效果图
动态1为:app效果图,动态2为:SharedPreferences存储的位置

存储内容的XML文件截图:

Android SharedPreferences(数据存储,需掌握)的更多相关文章
- Android之数据存储的五种方法
1.Android数据存储的五种方法 (1)SharedPreferences数据存储 详情介绍:http://www.cnblogs.com/zhangmiao14/p/6201900.html 优 ...
- Android中数据存储(一)
国庆没有给国家添堵,没有勾搭妹子,乖乖的写着自己的博客..... 本文将为大家介绍Android中数据存储的五种方式,数据存储可是非常重要的知识哦. 一,文件存储数据 ①在ROM存储数据 关于在ROM ...
- Android本地数据存储复习
Android本地数据存储复习 Android无论是应用层还是系统层都需要在本地保存一些数据,尤其在应用层中使用的就更为普遍,大体有这么几种:SharedPreference,file,sqlite数 ...
- android学习笔记45——android的数据存储和IO
android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...
- Android实现数据存储技术
转载:Android实现数据存储技术 本文介绍Android中的5种数据存储方式. 数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用Shar ...
- android中数据存储
android中数据存储 Android 中存储数据的方式有五种:SQLite数据库.文件存储.内容提供者.网络.SharedPreferences(Key----value)五种存储方式. ...
- Android中数据存储(四)——ContentProvider存储数据
目录(?)[+] 当一个应用程序在Android中安装后,我们在使用应用的过程中会产生很多的数据,应用都有自己的数据,那么我们应该如何存储数据呢? 数据存储方式 Android 的数据存储有5种方 ...
- Android中数据存储(三)——SQLite数据库存储数据
当一个应用程序在Android中安装后,我们在使用应用的过程中会产生很多的数据,应用都有自己的数据,那么我们应该如何存储数据呢? 数据存储方式 Android 的数据存储有5种方式: 1. Share ...
- 关于Android开发数据存储的方式(一)
关于Android开发数据存储方式(一) 在厦门做Android开发也有两个月了,快情人节了.我还在弄代码. 在微信平台上开发自己的APP,用到了数据存储的知识,如今总结一下: 整体的来讲.数据存储方 ...
随机推荐
- DataSet、DataTable转换List(泛型集合与DataSet互相转换 )
using System.Data; using System.Reflection; using System.Collections; using System.Collections.Gener ...
- FastDFS和apache/nginx整合
因为FastDFS默认自带的http服务器性能不好, 所以一般建议用外置的apache或者nginx 来解决http下载,以应付大并发的情况 注意nginx扩展模块只支持GET和HEAD模式获取文件, ...
- opennebula 开发记录
/app/opennebula/var//datastores/1/12933297f0ffeba3e55bbccabcb3153b to 127.0.0.1:/app/opennebula/data ...
- Docker学习笔记_删除某个镜像
实验:删除某个镜像 sudo docker rmi [Image ID] 1.查看镜像的ID sudo docker images 2.删除镜像 ...
- HttpSession相关API
//获取Session对象 request.getSession() request.getSession(boolean create) //获取SessionId getId() //获取当前se ...
- hdu 1556 Color the ball (线段树做法)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a ...
- 泊松分布 & 指数分布
一.泊松分布 日常生活中,大量事件是有固定频率的. 某医院平均每小时出生3个婴儿 某公司平均每10分钟接到1个电话 某超市平均每天销售4包xx牌奶粉 某网站平均每分钟有2次访问 它们的特点就是,我们可 ...
- Daubechies Wavelet
The Daubechies wavelets, based on the work of Ingrid Daubechies, are a family of orthogonal wavelets ...
- java类创建时里面成员执行的先后顺序
静态代码块在类第一次使用的时候执行一次,在构造函数执行之前执行.只要用到类,哪怕new对象(比如只声明变量)也会被执行,且只执行一次.一般用于对类进行初始化. 先执行静态代码块,静态成员(谁在前就先执 ...
- Go 的垃圾回收机制在实践中有哪些需要注意的地方(转)
在网上看到一篇非常好的文章http://www.zhihu.com/question/21615032,转载如下: go的gc还不完善但也不算不靠谱,关键看怎么用,尽量不要创建大量对象,也尽量不要频繁 ...