Android学习09
SharedPreferences
SharedPreferences,是一种轻量级的数据存储方式,采用Key/value的方式
进行映射,Sp通常用于记录一些参数配置、行为标记等!
1、获得mSharedPreferences的实例
mSharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
2、通过Eidt更新数据,获取一个Edit对象,所有对数据的操作都需要经过Edit
mEditor = mSharedPreferences.edit();
layout:
<?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"
android:padding="15dp"> <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:layout_marginTop="20dp"
android:text="保存"
android:textSize="20sp"/> <Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="显示"
android:textSize="20sp"/> <TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/> </LinearLayout>
Activity:
package com.example.helloworld.datastorage; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import com.example.helloworld.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_preferences2);
mEtName = findViewById(R.id.et_name);
mBtnSave = findViewById(R.id.btn_save);
mBtnShow = findViewById(R.id.btn_show);
mTvContent = findViewById(R.id.tv_content);
//获得mSharedPreferences的实例
mSharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
//获取一个Edit对象,所有对数据的操作都需要经过Edit
mEditor = mSharedPreferences.edit(); mBtnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//把EditText里的文本通过SharedPreferences保存起来
mEditor.putString("name",mEtName.getText().toString());//获取EditText里的文本内容
mEditor.commit();//提交,commmit才回生效 }
});
mBtnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取文件中的值
mTvContent.setText(mSharedPreferences.getString("name",""));
}
});
}
}
把EditText里的文本通过SharedPreferences保存起来,获取之后在EditText中显示
Android学习09的更多相关文章
- Android学习——windows下搭建Cygwin环境
在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配置,我们也讲到了在NDk r7以后,我 ...
- android学习系列:jercy——AI3 的博客
[android学习之十七]——特色功能2:桌面组件(快捷方式,实时文件夹) 二.桌面组件 1.快捷方式 Android手机上得快捷方式的意思可以以我们实际PC机器上程序的快捷方式来理解.而andro ...
- Android学习链接大放送
虽然贴链接这种事情..真是一种很偷懒的做法... 但是我一个小菜鸟,果断还是要以多向别人学习为主... 好资源要和大家分享对不对! 况且..放博客里..比收藏夹的利用几率要大一点! 原作者应该也很喜欢 ...
- Android学习系列(17)--App列表之圆角ListView(续)
http://www.cnblogs.com/qianxudetianxia/archive/2011/09/19/2068760.html 本来这篇文章想并到上篇Android学习系列(16)- ...
- Android学习路线总结,绝对干货
title: Android学习路线总结,绝对干货 tags: Android学习路线,Android学习资料,怎么学习android grammar_cjkRuby: true --- 一.前言 不 ...
- Android 学习资源
下面这些资源对Android开发来说是很有帮助的! 最常用的: Android开发官方网站:http://developer.android.com/index.html 这个网站应该是Android ...
- Android学习资料收集
1.Android 学习之路 http://stormzhang.com/android/2014/07/07/learn-android-from-rookie/
- Android学习——第一个NDK程序
在前面的学习中,我们已经讲解了关于NDK编程的环境搭建流程,简单的使用我们也通过官网本身自带的例子进行说明了.可是相信大家一定还存在这么的一个疑惑:“如果我要自己利用NDK编写一个Android应用, ...
- Android学习——windows下搭建NDK_r9环境
1. NDK(Native Development Kit) 1.1 NDK简介 Android NDK是一套允许开发人员使用本地代码(如C/C++)进行Android APP功能开发的工具,通过这个 ...
随机推荐
- Energy Consumption Of Low-Pressure Crystal Craft Lights
What kind of place is the low-pressure crystal light generally suitable for? Low-pressure crystal li ...
- 训练20191007 2017-2018 ACM-ICPC Latin American Regional Programming Contest
2017-2018 ACM-ICPC Latin American Regional Programming Contest 试题地址:http://codeforces.com/gym/101889 ...
- WPF:MVVM模式下ViewModel调用View
两种基本方法: 消息通知和参数传递 一.消息通知 利用View里的IsEnable属性 原理是这样的: 1.UI中的IsEnabled绑定VM中的属性 2.UI的后台代码中,注册IsEnableCha ...
- python面试的100题(2)
def print_directory_contents(sPath): """ 这个函数接收文件夹的名称作为输入参数 返回该文件夹中文件的路径 以及其包含文件夹中文件的 ...
- AcWing 开平方 浮点数二分
#include<iostream> using namespace std; int main() { double x; cin>>x; ,r=x; ) //for(int ...
- C语言 strlen
C语言 strlen #include <string.h> size_t strlen(const char *s); 功能:计算指定指定字符串s的长度,不包含字符串结束符‘\0’ 参数 ...
- 512,a标签的target属性
<a> 标签的 target 属性规定在何处打开链接文档. 1.“_blank”的意思: 浏览器总在一个新打开.未命名的窗口中载入目标文档. 2.“_parent”的意思: 这个目标,使得 ...
- awk从放弃到入门(3):awk变量
一.变量概述 对于awk来说"变量"又分为"内置变量" 和 "自定义变量" , "输入分隔符FS"和"输出分隔 ...
- php curl请求 header头携带参数
$headers = array( 'api-key:'.$key, 'authorization:'.$authorization, ); //初始化 $curl = c ...
- buuctf
大白 | png图片改高度png图片改高度[外链图片转存失败(img-PojN2D3v-1567086301372)(evernotecid://74A3E6DA-E009-4797-AA60-5DE ...
