SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出……。下面通过一个小小的案例,分享一下我之前做的。

1、最终效果图

大致就是通过SharedPreferences存储类创建一个配置文件(这里是通过按钮去触发的),然后向配置文件中写入配置信息,最后就是读配置中“键”对应的“值”(这里通过按钮触发将读到的值显示出来)。还是比较简单,很容易的。

2、布局文件

activity_main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="thonlon.example.cn.sharedpreferencesdemo.MainActivity"> <Button
android:id="@+id/btn_create"
android:layout_width="396dp"
android:layout_height="46dp"
android:text="@string/btn_create_sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" /> <Button
android:id="@+id/btn_getInfo"
android:layout_width="396dp"
android:layout_height="46dp"
android:layout_marginLeft="0dp"
android:text="@string/btn_getInfo_sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1" /> <TextView
android:id="@+id/textView1"
android:layout_width="80dp"
android:layout_height="20dp"
android:background="@color/colorPrimary"
android:text="英文显示:"
android:textAlignment="center"
android:textColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.465" /> <TextView
android:id="@+id/textView2"
android:layout_width="80dp"
android:layout_height="20dp"
android:background="@color/colorPrimary"
android:text="中文显示:"
android:textAlignment="center"
android:textColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.205" /> <TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.244" /> <TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.53" />
</android.support.constraint.ConstraintLayout>

3、activity文件

MainActivity.java

 package thonlon.example.cn.sharedpreferencesdemo;

 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.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { private SharedPreferences sharedPreferences;
private Button btn_create, btn_getInfo;
private TextView textView1, textView2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_create = (Button) findViewById(R.id.btn_create);
btn_getInfo = (Button) findViewById(R.id.btn_getInfo);
textView1 = (TextView) findViewById(R.id.tv1);
textView2 = (TextView) findViewById(R.id.tv2);
// 设置配置文件的名称和配置文件的被访问权限
sharedPreferences = getSharedPreferences("config", MODE_ENABLE_WRITE_AHEAD_LOGGING);
btn_create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
write();
} private void write() {
//得到配置编辑器
SharedPreferences.Editor edit = sharedPreferences.edit();
//写入配置信息到配置文件中
edit.putString("Chinese", "SharedPreferences是Android平台上一个轻量级的存储类。");
edit.putString("English", "SharedPreferences is a lightweight storage class on the Android platform to save some of the common configuration of the application.");
//注意以上只是将配置信息写入了内存
edit.commit();//提交内存配置信息到本地
Toast.makeText(getApplicationContext(), "成功创建文件", Toast.LENGTH_LONG).show();
}
});
btn_getInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
read();
} private void read() {
String chinese_info = sharedPreferences.getString("Chinese", "");
String english_info = sharedPreferences.getString("English", "");
textView1.setText(chinese_info);
textView2.setText(english_info);
}
});
}
}

4、源码下载

百度云下载链接:https://pan.baidu.com/s/1PRY5fdlAt5ZSl05NGniWrw 密码:tpr0

Android中SharedPerforences的简单使用示例 --Android基础的更多相关文章

  1. [原创]Android中LocationManager的简单使用,获取当前位置

    Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...

  2. 解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题

    解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题http ...

  3. Android中ProgressDialog的简单示例

    网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下: 建立android工程等工作都略去,Google一下就可以了. 下面来介绍主 ...

  4. android中的回调简单认识

    首先说一下最抽象的形式--2个类,A类和B类.A类含有1个接口.1个接口变量.(可能含有)1个为接口变量赋值的方法以及1个会使用接口变量的"地方";B类实现A中的接口,(可能)含有 ...

  5. Android中Tomcat的简单配置和使用

    因为学Android已经有一段时间了,但是在学校,服务器方面是个短板啊,没有专门的服务器拿给我们学生练手,所以只有自己找办法了.当然,Tomcat就是不二的选择了. 在网上看了看资料,还是觉得自己记录 ...

  6. Android笔记(二十八) Android中图片之简单图片使用

    用户界面很大程度上决定了APP是否被用户接收,为了提供友好的界面,就需要在应用中使用图片了,Android提供了丰富的图片处理功能. 简单使用图片 使用Drawable对象 为Android应用增加了 ...

  7. iOS 和Android中的正则表达式简单使用

    ios 中需要使用NSRegularExpression类,NSTextCheckingResult类. 下面给出最基本的实现代码 NSRegularExpression *regex = [NSRe ...

  8. Android中Fragment的简单介绍

    Android是在Android 3.0 (API level 11)引入了Fragment的,中文翻译是片段或者成为碎片(个人理解),可以把Fragment当成Activity中的模块,这个模块有自 ...

  9. Android中SharePreferences的简单实现

    Android中提供SharePreferences这种轻量级的数据存储模式,这种模式能够存储少量数据,并能为自身和其他应用提供数据接口.相对于其他数据存储方式,SharePreferences更加轻 ...

随机推荐

  1. Codeforces 822D My pretty girl Noora - 线性筛 - 动态规划

    In Pavlopolis University where Noora studies it was decided to hold beauty contest "Miss Pavlop ...

  2. Java.util.properties读取配置文件分析

    Java.util.properties API链接: https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html Clas ...

  3. git常见使用场景总结

    1.怎么回退到之前的commit? 用git reset --hard HEAD回退到最新提交的版本 用git reset --hard HEAD^回退到上一个版本 用git reset --hard ...

  4. Docker 入门指南——Dockerfile 指令

    COPY 复制文件 格式: COPY [--chown=<user>:<group>] <源路径>... <目标路径> 源路径可以是多个,甚至可以使通配 ...

  5. 螺旋折线(可能是最简单的找规律)【蓝桥杯2018 C/C++ B组】

    标题:螺旋折线 如图p1.png所示的螺旋折线经过平面上所有整点恰好一次.   对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度. 例如dis(0 ...

  6. HDU 6406 Taotao Picks Apples & FJUT3592 做完其他题后才能做的题(线段树)题解

    题意(FJUT翻译HDU): 钱陶陶家门前有一棵苹果树. 秋天来了,树上的n个苹果成熟了,淘淘会去采摘这些苹果. 到园子里摘苹果时,淘淘将这些苹果从第一个苹果扫到最后一个. 如果当前的苹果是第一个苹果 ...

  7. 【原理、应用】Quartz集群原理及配置应用

    一.Quartz任务调度的基本实现原理 Quartz是OpenSymphony开源组织在任务调度领域的一个开源项目,完全基于Java实现.作为一个优秀的开源调度框架,Quartz具有以下特点: 强大的 ...

  8. P3159 [CQOI2012]交换棋子

    思路 相当神奇的费用流拆点模型 最开始我想到把交换黑色棋子看成一个流流动的过程,流从一个节点流向另一个节点就是交换两个节点,然后把一个位置拆成两个点限制流量,然后就有了这样的建图方法 S向所有初始是黑 ...

  9. 《剑指Offer 1.二维数组中的查找》2019-03-25

    剑指Offer  第一题 题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数 ...

  10. 【C#】异步的用法

    1. C#5.0 加入了async, await关键字. async是在声明异步方法时使用的修饰符, 声明放在返回值之前即可,await表达式则负责消费异步操作, 不能出现在catch或finally ...