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. 1、简单的BackGroundWorker多线程时时刷新UI界面,并显示进度

    BackGroundWorker是微软提供的封装好了的,非常实用的控件,我们可以在控件中将其拖到Winform之中,然后简单的系统生成代码式的编辑事件处理. 以下是,比较经典且简单的实用,后面的一篇较 ...

  2. 用JS实现任意导航栏的调用

    首先设计一个关于导航的层叠样式表如:body{font-size:12px;font-family:Arial,Helvetica,'宋体',sans-serif;color:#333;backgro ...

  3. Mac新建文件夹、txt文件、其他格式文件

    Mac新建txt,正好有人问我,我就把我自己的方法记录一下: 先cd到你指定的文件路径下: 新建文件夹: mkdir test 新建txt touch test.txt 新建无后缀格式文件 touch ...

  4. C++知识整理(进制)

    ++输出二进制.十进制.八进制和十六进制总结 分类: C++ 2013-01-14 02:26 592人阅读 评论(0) 收藏 举报 在C++中,默认状态下,数据按十进制输入输出.如果要求按八进制或十 ...

  5. 阿里云-DRDS(转)

    分库分表 DRDS 在后端将数据量较大的数据表水平拆分到后端的每个 RDS 数据库中,这些拆分到RDS中的数据库被称为分库,分库中的表称为分表.DRDS 由每个分库负责每一份数据的读写操作,从而有效的 ...

  6. mysql 权限处理

    这是对mysql 业务用户在权限处理中遇到的坑: 之前在新建mysql 实例后会做两件事 1.增加业务库 2.为业务库增加一个与之对应的用户 create database appdb char se ...

  7. Linux下的ip命令

    linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...

  8. Mac升级bash到最新版本

    mac自带的bash为3.2版本,而最新的bash是4.9,需要升级了,才能支持关联数组等新特性. 1.brew install bash 2.安装到/usr/local/bin/bash里面.可以通 ...

  9. verilog中的default应该赋什么样的值

    Q:在状态机的case语句中,最后要加上默认项default,可是我看到有的书上写的是一个确定的状态,有的则是不定态xxx,到底应该写那个啊?求助! A1:取决于case条件是否完备啦如果你的case ...

  10. 每日英语:The Biggest Distraction In The Office Is Sitting Next To You

    The big push in office design is forcing co-workers to interact more. Cubicle walls are lower, offic ...