xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_1"
android:hint="key"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_2"
android:hint="value"
/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存"
android:layout_weight="1"
android:onClick="baocunonclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="获取"
android:layout_weight="1"
android:onClick="huoquonclick"/> </LinearLayout>
</LinearLayout>

java

package com.example.chenshuai.test321;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class Activitydata extends AppCompatActivity { EditText et_1;
EditText et_2;
SharedPreferences sp;//不能通过set方法存,通过edit存数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitydata); et_1 = (EditText)findViewById(R.id.et_1);
et_2 = (EditText)findViewById(R.id.et_2); //1.获取SP的实例,指定了文件名和操作模式
sp = getSharedPreferences("mydata",MODE_PRIVATE);
} //保存
public void baocunonclick(View view)
{
//1.获取key和value
String key = et_1.getText().toString();
String value = et_2.getText().toString(); if (key.length()==0 || value.length()==0)
{
Toast.makeText(Activitydata.this, "key或value不为空", Toast.LENGTH_SHORT).show();
} else {
//2.获取Editor
SharedPreferences.Editor editor = sp.edit(); //3.放入键值对
editor.putString(key, value); //4.提交保存
boolean b = editor.commit();
if (b) {
Toast.makeText(Activitydata.this, "保存成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Activitydata.this, "失败", Toast.LENGTH_SHORT).show();
}
}
} //获取
public void huoquonclick(View view)
{
//1.获取要读的key
String key = et_1.getText().toString(); //2.读并设置文本框
et_2.setText(sp.getString(key,"没有发现key")); }
}

Andriod——数据存储 SharedPrefrences的更多相关文章

  1. Android之文件数据存储

    一.文件保存数据介绍 Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过程与在J2SE环境中保存数据到文件中是一样的.文件可用来存放大量数据,如文本.图 ...

  2. Android系统的五种数据存储形式(一)

    Android系统有五种数据存储形式,分别是文件存储.SP存储.数据库存储.contentprovider 内容提供者.网络存储.其中,前四个是本地存储.存储的类型包括简单文本.窗口状态存储.音频视频 ...

  3. Android中数据存储之SharedPreferences

    import android.content.Context; import android.content.SharedPreferences; import android.content.Sha ...

  4. 安卓数据存储(3):SQLite数据库存储

    SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级:使用 SQLit ...

  5. Kooboo CMS技术文档之三:切换数据存储方式

    切换数据存储方式包括以下几种: 将文本内容存储在SqlServer.MySQL.MongoDB等数据库中 将站点配置信息存储在数据库中 将后台用户信息存储在数据库中 将会员信息存储在数据库中 将图片. ...

  6. Android之数据存储的五种方法

    1.Android数据存储的五种方法 (1)SharedPreferences数据存储 详情介绍:http://www.cnblogs.com/zhangmiao14/p/6201900.html 优 ...

  7. Android之网络数据存储

    一.网络保存数据介绍 可以使用网络来保存数据,在需要的时候从网络上获取数据,进而显示在App中. 用网络保存数据的方法有很多种,对于不同的网络数据采用不同的上传与获取方法. 本文利用LeanCloud ...

  8. Android之SharedPreferences数据存储

    一.SharedPreferences保存数据介绍 如果有想要保存的相对较小键值集合,应使用SharedPreferences API.SharedPreferences对象指向包含键值对的文件并提供 ...

  9. Atitit 数据存储视图的最佳实际best practice attilax总结

    Atitit 数据存储视图的最佳实际best practice attilax总结 1.1. 视图优点:可读性的提升1 1.2. 结论  本着可读性优先于性能的原则,面向人类编程优先于面向机器编程,应 ...

随机推荐

  1. HTTP1.1协议请求方面参数

    请求信息 GET / HTTP/1.1                                              ->请求行 Accept: */* Accept-Languag ...

  2. nyoj------------找球号(一)

    找球号(一) 时间限制:3000 ms  |           内存限制:65535 KB 难度:3   描述 在某一国度里流行着一种游戏.游戏规则为:在一堆球中,每个球上都有一个整数编号i(0&l ...

  3. 使用 HTML5 History 新特性增强 Ajax 的体验(转)

    一. 场景再现 如大家熟知,Ajax 可以实现页面的无刷新操作,但会造成两个与普通页面操作(有刷新地改变页面)有着明显差别的问题—— URL 没有修改以及无法使用前进.后退按钮.例如常见的 Ajax ...

  4. Android开发学习之浅谈显示Intent和隐式Intent

    Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...

  5. Win32:引用头文件

    1.首先,在代码文件头部使用#include来包含任何头文件(扩展名为.h或者.hpp的文件)都不会“产生”额外的函数. 我们可以说,包含一个头文件之后,该头文件内部已经定义的一些变量,宏,函数等等资 ...

  6. Python 列表 reverse() 方法

    描述 Python 列表 reverse() 方法对列表中的元素进行反向排序. 语法 reverse() 方法语法: L.reverse() 参数 无. 返回值 该方法没有返回值,但是会对列表的元素进 ...

  7. django1.8高级视图和URL配置读书笔记

    一.在url配置中可以通过导入视图函数来将url模式和对应的函数对象进行映射,也可以通过字符串的形式进行映射.字符串包含应当是模块名.函数名的组合例如: 之前: from mysite import ...

  8. Java中弹出框的集中方式

    1.显示一个错误对话框,该对话框显示的 message 为 'alert': JOptionPane.showMessageDialog(null, "alert", " ...

  9. 【OC语法快览】二、存取方法

    Accessors 存取方法 All instance variables are private in Objective-C by default, so you should use acces ...

  10. 使用perldoc阅读perl文档

    perl在安装的时候,就给我们送上一份大礼,组织精美,解释详细的perl百科全书已经安装在你的电脑里面了,遇到问题不要在去搜索那些博客了,还是练练英文,看看perldoc吧,呵呵. 1.用perldo ...