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. HDUOJ----(1030)Delta-wave

    Delta-wave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

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

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

  3. iOS开发之多媒体播放

    iOS开发之多媒体播放 iOS sdk中提供了很多方便的方法来播放多媒体.本文将利用这些SDK做一个demo,来讲述一下如何使用它们来播放音频文件. AudioToolbox framework 使用 ...

  4. Python tuple 函数

    描述 Python 元组 tuple() 函数将列表转换为元组. 语法 以下是 tuple 的语法: tuple( seq ) 参数 seq -- 要转换为元组的序列. 返回值 返回元组. 实例 以下 ...

  5. C#实现相似QQ的隐藏浮动窗口、消息闪动

    功能简单介绍 当语音客服系统登录成功进入主界面时,本聊天工具将会自己主动隐藏在左下角位置,当鼠标移动到左下角时,自己主动弹出,当鼠标移开聊天窗口时,自己主动隐藏.假设想让聊天窗口固定在桌面.仅仅要拖动 ...

  6. cocos2dx 3.1从零学习(五)——动画

    动画是游戏中最重要的表现部分,本篇仅仅是初步学习大部分动画的用法.没有什么原理性的东西,可是样例有非常多,假设有不熟的地方要练一下. 特别是Spawn和Sequence的组合使用,什么时候使用Spaw ...

  7. win10 U盘安装ubuntu16.04双系统

    所需工具U盘,软件ultralISO.ubuntu16.04,自己使用的系统是win10 一.制作U盘启动盘 打开ultraISO软件 2 2  3 4 开始写入—>直到完成大概五分的样子 二. ...

  8. jquery.dataTables的用法

    写页面前端时,使用表格的插件可以快速漂亮的排版.本例子中使用jquery.dataTables来处理table.直接来点干货 html代码如下 <table cellpadding=" ...

  9. 在为shader program pass uniform之前一定要先use program。

    在为shader program pass uniform之前一定要先use program.

  10. POI导出

    public void export(){try {String columns = getPara("nameArray");List<Record> list = ...