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. 工具-Memcahce和Redis比较

    一.Memcache 1.     memecache 把数据全部存在内存之中,断电后会挂掉,数据不能超过内存大小redis有部份存在硬盘上,这样能保证数据的持久性. 2.      Memcache ...

  2. this.class.getClassLoader().getResourceAsStream与this.class.getResourceAsStream

    本文部分转自:http://xixinfei.iteye.com/blog/1256291 this.getClass().getClassLoader().getResource("tem ...

  3. Python实现天数倒计时计算

    tips:在datetime模块里有一个计算时间差的 timedelta.让两个datetime对象相减就得到timedelta ###--Python实现天数倒计时计算 #tips:在datetim ...

  4. UltraEdit加入到右键菜单中

    http://www.cppblog.com/prayer/archive/2009/02/20/74429.htmlUltraEdit安装好之后,拷贝到其它机器就可以直接使用而无需注册,但少了一个功 ...

  5. C# MediaPlayer的详细用法

    AxWindowsMediaPlayer的详细用法 作者:龙昊雪 AxWindowsMediaPlayer的详细用法收藏 function StorePage(){d=document;t=d.sel ...

  6. oc 目录结构

    最顶级的oc2是一个项目名,下级的oc2下面的文件是一些程序文件(.m或者.h的文件)和一些界面文件(.xib或者storyboard文件),supporting files文件下面的是资源文件,可以 ...

  7. BIP_BI Pubisher的基本语法(概念)

    2014-05-31 Created By BaoXinjian

  8. html中如何让table显示的更好

    在html文件编写中,经常使用到table来做一些表格.如何让它显示的更像一张表格?接下来为你讲解. 基本格式 <div> <th>我的一张表格</th> < ...

  9. ECSHOP后台权限分配原理分析

    1.在权限管理->管理员列表,中添加一名管理员 2.在admin\includes\inc_menu.php $modules['02_cat_and_goods']['01_goods_lis ...

  10. Linux常用命令的解释

    作者博客已转移,http://www.daniubiji.cn/archives/25 Linux简介及Ubuntu安装 常见指令 系统管理命令 打包压缩相关命令 关机/重启机器 Linux管道 Li ...