Andriod——数据存储 SharedPrefrences
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的更多相关文章
- Android之文件数据存储
一.文件保存数据介绍 Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过程与在J2SE环境中保存数据到文件中是一样的.文件可用来存放大量数据,如文本.图 ...
- Android系统的五种数据存储形式(一)
Android系统有五种数据存储形式,分别是文件存储.SP存储.数据库存储.contentprovider 内容提供者.网络存储.其中,前四个是本地存储.存储的类型包括简单文本.窗口状态存储.音频视频 ...
- Android中数据存储之SharedPreferences
import android.content.Context; import android.content.SharedPreferences; import android.content.Sha ...
- 安卓数据存储(3):SQLite数据库存储
SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级:使用 SQLit ...
- Kooboo CMS技术文档之三:切换数据存储方式
切换数据存储方式包括以下几种: 将文本内容存储在SqlServer.MySQL.MongoDB等数据库中 将站点配置信息存储在数据库中 将后台用户信息存储在数据库中 将会员信息存储在数据库中 将图片. ...
- Android之数据存储的五种方法
1.Android数据存储的五种方法 (1)SharedPreferences数据存储 详情介绍:http://www.cnblogs.com/zhangmiao14/p/6201900.html 优 ...
- Android之网络数据存储
一.网络保存数据介绍 可以使用网络来保存数据,在需要的时候从网络上获取数据,进而显示在App中. 用网络保存数据的方法有很多种,对于不同的网络数据采用不同的上传与获取方法. 本文利用LeanCloud ...
- Android之SharedPreferences数据存储
一.SharedPreferences保存数据介绍 如果有想要保存的相对较小键值集合,应使用SharedPreferences API.SharedPreferences对象指向包含键值对的文件并提供 ...
- Atitit 数据存储视图的最佳实际best practice attilax总结
Atitit 数据存储视图的最佳实际best practice attilax总结 1.1. 视图优点:可读性的提升1 1.2. 结论 本着可读性优先于性能的原则,面向人类编程优先于面向机器编程,应 ...
随机推荐
- C#编写的 8种初级+高级排序方法(转)
摘自:http://blog.csdn.net/mevin/article/details/6714520 程序代码: view plaincopy to clipboard using System ...
- HDUOJ----专题训练C
Problem C Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- iOS与H5交互遇到的坑
之前的博客写过使用<JavaScriptCore/JavaScriptCore.h>库来实现与H5的交互,但是在项目中还是遇到了一些不得不踩的坑.在这里将我遇到的问题以及参考网上几位大神的 ...
- 10个你必须知道的jQueryMobile代码片段(转)
1.在列表项和按钮上禁用文本截断 如果你的列表项或者按钮上是一个很长的文本,它将会被jQuery Mobile自动截断,要禁用这个截断设置,需要在CSS选择器上添加属性"white ...
- Jersey框架
我从别人博客那儿搬点东西过来,原博请看最下面~看的顺序反了..应该先看JAX-RS整体的东西再看具体实现的Jersey例子的= =无数次改这个日记了不能忍...所以决定把JAX-RS系列的文章搬过来. ...
- OpenXml读取word内容注意事项
OpenXml读取word内容注意事项 1.使用OpenXml读取word内容,word后缀必须是".docx":如果word后缀是".doc"需要转成&quo ...
- RAC安装gird,第一个节点执行root.sh报"The ora.asm resource is not ONLINE"错误
RAC版本:11.2.0.4 OS版本:linux 6.4 RAC安装gird,第一个节点执行root.sh运行失败,报"The ora.asm resource is not ONLINE ...
- 关于Andorid的RecyclerView在V7包下找不到的解决办法
关于Andorid的RecyclerView在V7包下找不到的解决办法 最近在学习使用RecyclerView替换现有的ListView,看了几篇文章.当准备自己动手实现的时候发现,V7包下找不到 ...
- 关于c语言内存分配,malloc,free,和段错误,内存泄露
1. C语言的函数malloc和free (1) 函数malloc和free在头文件<stdlib.h>中的原型及参数 void * malloc(size_t size ...
- http连接优化
http连接的性能优化 并行连接(能够同一时候和多台server建立HTTP连接) 持久连接 管道化连接 复用的连接 并行连接 长处: 并行连接能够在带宽资源充足的情况下同一时候建立多个HTTP连接, ...