Android [SharedPreference轻量级存储]
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轻量级存储]的更多相关文章
- Android SharedPreference 数据存储
参考:http://www.cnblogs.com/friends-wf/p/4835818.html 应用开发过程中,数据存储几乎是肯定会遇到的问题,根据要存储的数据类型和数量,可以选择合适的存储方 ...
- android SharedPreferences 轻量级存储!
首先在当前进程也就是当前的项目里面进行存储 SharedPreferences.Editor editor = mContext.getSharedPreferences("tvplay&q ...
- Android本地数据存储复习
Android本地数据存储复习 Android无论是应用层还是系统层都需要在本地保存一些数据,尤其在应用层中使用的就更为普遍,大体有这么几种:SharedPreference,file,sqlite数 ...
- Android中数据存储(一)
国庆没有给国家添堵,没有勾搭妹子,乖乖的写着自己的博客..... 本文将为大家介绍Android中数据存储的五种方式,数据存储可是非常重要的知识哦. 一,文件存储数据 ①在ROM存储数据 关于在ROM ...
- Android SharedPreference的使用
在<Android 在内部存储读写文件>一文中,谈到了登录用户名和密码的方法,通过读取内存文件来实现,但是会出现问题,因为登录名和密码的形式通过username##password的形式, ...
- android学习笔记45——android的数据存储和IO
android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...
- Android实现数据存储技术
转载:Android实现数据存储技术 本文介绍Android中的5种数据存储方式. 数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用Shar ...
- Android中数据存储(三)——SQLite数据库存储数据
当一个应用程序在Android中安装后,我们在使用应用的过程中会产生很多的数据,应用都有自己的数据,那么我们应该如何存储数据呢? 数据存储方式 Android 的数据存储有5种方式: 1. Share ...
- Android本地数据存储: ASimpleCache
一:前言 在上一篇博客Android本地数据存储: Reservoir 博客中,我提到,除了Reservoir库,还可以采用ASimpleCache开源库,来实现本地数据存储.昨天并没有仔细的对比Re ...
随机推荐
- python 31 升级版解决粘包现象
目录 1. recv 工作原理 2.升级版解决粘包问题 3. 基于UDP协议的socket通信 1. recv 工作原理 1.能够接收来自socket缓冲区的字节数据: 2.当缓冲区没有数据可以读取时 ...
- 初尝RabbitMQ消息队列
RabbitMQ 是什么? 消息中间件 作用? 用于分布式项目中的模块解耦 用法? 创建队列 创建消息工厂并设置 (生产者额外步骤 : 创建消息) 创建连接,通道 声明队列 生产者 : 发送消 ...
- 猫眼电影和电影天堂数据csv和mysql存储
字符串常用方法 # 去掉左右空格 'hello world'.strip() # 'hello world' # 按指定字符切割 'hello world'.split(' ') # ['hello' ...
- .NET Core 学习资料精选:进阶
.NET 3.0 这个月就要正式发布了,对于前一篇博文<.NET Core 学习资料精选:入门>大家学的可还开心?这是本系列的第二篇文章:进阶篇,喜欢的园友速度学起来啊. 对于还在使用传统 ...
- 使用SpringSecurity保护程序安全
首先,引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- ObjectMapper
String jsonStr=""; String content=jsonStr; ObjectMapper objectMapper = new ObjectMapper(); ...
- pickle 序列化对象
# 序列化对象 import pickle mylist=[[1,2,3,4,5,6,7],["abc","xyz","hello"],[1 ...
- python修改内存,(修改植物大战僵尸)
import win32process # 进程模块 import win32con # 系统定义 import win32api # 调用系统模块 import ctypes # c语言类型 imp ...
- SDU暑期集训排位(9)
SDU暑期集训排位(9) G. Just Some Permutations 基础 DP 练习部分 定义 \(f(S)\),表示让 S 中的人全 happy 的方案数. \(dp[i][j]\) 表示 ...
- 【Windows】PostgreSql安装
Installer安装包问题 Problem running post-install step. Installation may not complete correctly. The datab ...