SharedPreferencesActivity.java

package com.xdw.a122.data;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import com.xdw.a122.R; public class SharedPreferencesActivity extends AppCompatActivity {
private EditText mEtName;
private Button mBtnSave,mBtnShow;
private TextView mTvContent;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
mEtName=findViewById(R.id.et_name);
mBtnSave=findViewById(R.id.btn_save);
mBtnShow=findViewById(R.id.btn_show);
mTvContent=findViewById(R.id.tv_show); mSharedPreferences=getSharedPreferences("data",MODE_PRIVATE); //名称和类型
mEditor=mSharedPreferences.edit(); mBtnSave.setOnClickListener(new View.OnClickListener() { //存储
@Override
public void onClick(View v) {
mEditor.putString("name",mEtName.getText().toString());
//此处写要提交的内容
mEditor.apply();
//or commit(); 存储完成
}
});
mBtnShow.setOnClickListener(new View.OnClickListener() { //读取
@Override
public void onClick(View v) {
mTvContent.setText(mSharedPreferences.getString("name",""));
}
});
}
}

activity_shared_preferences

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".data.SharedPreferencesActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入内容"
/>
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示"/>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>

输入内容在 //存储 输入存储内容

并在tv_show显示

mSharedPreferences=getSharedPreferences("data",MODE_PRIVATE);  //名称和类型
mEditor=mSharedPreferences.edit();

存储的类型

Android [SharedPreference轻量级存储]的更多相关文章

  1. Android SharedPreference 数据存储

    参考:http://www.cnblogs.com/friends-wf/p/4835818.html 应用开发过程中,数据存储几乎是肯定会遇到的问题,根据要存储的数据类型和数量,可以选择合适的存储方 ...

  2. android SharedPreferences 轻量级存储!

    首先在当前进程也就是当前的项目里面进行存储 SharedPreferences.Editor editor = mContext.getSharedPreferences("tvplay&q ...

  3. Android本地数据存储复习

    Android本地数据存储复习 Android无论是应用层还是系统层都需要在本地保存一些数据,尤其在应用层中使用的就更为普遍,大体有这么几种:SharedPreference,file,sqlite数 ...

  4. Android中数据存储(一)

    国庆没有给国家添堵,没有勾搭妹子,乖乖的写着自己的博客..... 本文将为大家介绍Android中数据存储的五种方式,数据存储可是非常重要的知识哦. 一,文件存储数据 ①在ROM存储数据 关于在ROM ...

  5. Android SharedPreference的使用

    在<Android 在内部存储读写文件>一文中,谈到了登录用户名和密码的方法,通过读取内存文件来实现,但是会出现问题,因为登录名和密码的形式通过username##password的形式, ...

  6. android学习笔记45——android的数据存储和IO

    android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...

  7. Android实现数据存储技术

    转载:Android实现数据存储技术 本文介绍Android中的5种数据存储方式. 数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用Shar ...

  8. Android中数据存储(三)——SQLite数据库存储数据

    当一个应用程序在Android中安装后,我们在使用应用的过程中会产生很多的数据,应用都有自己的数据,那么我们应该如何存储数据呢? 数据存储方式 Android 的数据存储有5种方式: 1. Share ...

  9. Android本地数据存储: ASimpleCache

    一:前言 在上一篇博客Android本地数据存储: Reservoir 博客中,我提到,除了Reservoir库,还可以采用ASimpleCache开源库,来实现本地数据存储.昨天并没有仔细的对比Re ...

随机推荐

  1. 关于多线程中sleep、join、yield的区别

    好了.说了多线程,那就不得不说说多线程的sleep().join()和yield()三个方法的区别啦 1.sleep()方法 /** * Causes the currently executing ...

  2. keras的get_value运行越来越慢的问题解决

    https://blog.csdn.net/mingshili/article/details/81941677?utm_source=blogxgwz5

  3. Mobx-React : 当前适合React的状态管理工具

    MobX 简单.可扩展的状态管理        MobX 是由 Mendix.Coinbase.Facebook 开源和众多个人赞助商所赞助的.    安装 安装: npm install mobx ...

  4. 约瑟夫环问题:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

    首先,我最大的学习来源不是百度而是我群友~~在这里表白一波我热爱学习的群友们!然后今天群里突然有人提出了题目的这个问题:有n个人围成一圈,顺序排号.从第一个人开始报数(从1到3报数),凡报到3的人退出 ...

  5. 洛谷 P1135 【奇怪的电梯】

    题库 :洛谷 题号 :1135 题目 :奇怪的电梯 link :https://www.luogu.org/problemnew/show/P1135 一. 动态规划 : 思路 :这道题用动规来解决其 ...

  6. 一本通 P1486 【黑暗城堡】

    题库 :一本通 题号 :1486 题目 :黑暗城堡 link :http://ybt.ssoier.cn:8088/problem_show.php?pid=1486 思路 :这道题既然要求使加入生成 ...

  7. CodeForces - 697C-Lorenzo Von Matterhorn(有点像LCA,原创

    传送门: CodeForces - 697C 原创--原创--原创 第一次自己A了一道感觉有点难度的题: 题意:在一个类似于二叉树的图上,1 : u ,v,w 表示从u到v的所以路都加上w的费用: 2 ...

  8. lightoj 1158 - Anagram Division(记忆化搜索+状压)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1158 题解:这题看起来就像是记忆搜索,由于s很少最多就10位所以可以考虑用状压 ...

  9. POJ1833 排列 调用全排列函数 用copy函数节省时间 即使用了ios同步代码scanf还是比较快

    排列 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21268   Accepted: 8049 Description 题 ...

  10. 【Offer】[10-2] 【青蛙跳阶问题】

    题目描述 思路分析 Java代码 代码链接 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 思路分析 其实就是斐波那契 ...