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

1、最终效果图

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

2、布局文件

activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="thonlon.example.cn.sharedpreferencesdemo.MainActivity">
  8.  
  9. <Button
  10. android:id="@+id/btn_create"
  11. android:layout_width="396dp"
  12. android:layout_height="46dp"
  13. android:text="@string/btn_create_sp"
  14. app:layout_constraintBottom_toBottomOf="parent"
  15. app:layout_constraintLeft_toLeftOf="parent"
  16. app:layout_constraintRight_toRightOf="parent"
  17. app:layout_constraintTop_toTopOf="parent"
  18. app:layout_constraintVertical_bias="0.0" />
  19.  
  20. <Button
  21. android:id="@+id/btn_getInfo"
  22. android:layout_width="396dp"
  23. android:layout_height="46dp"
  24. android:layout_marginLeft="0dp"
  25. android:text="@string/btn_getInfo_sp"
  26. app:layout_constraintBottom_toBottomOf="parent"
  27. app:layout_constraintLeft_toLeftOf="parent"
  28. app:layout_constraintRight_toRightOf="parent"
  29. app:layout_constraintTop_toTopOf="parent"
  30. app:layout_constraintVertical_bias="0.1" />
  31.  
  32. <TextView
  33. android:id="@+id/textView1"
  34. android:layout_width="80dp"
  35. android:layout_height="20dp"
  36. android:background="@color/colorPrimary"
  37. android:text="英文显示:"
  38. android:textAlignment="center"
  39. android:textColor="@color/colorWhite"
  40. app:layout_constraintBottom_toBottomOf="parent"
  41. app:layout_constraintHorizontal_bias="0.0"
  42. app:layout_constraintLeft_toLeftOf="parent"
  43. app:layout_constraintRight_toRightOf="parent"
  44. app:layout_constraintTop_toTopOf="parent"
  45. app:layout_constraintVertical_bias="0.465" />
  46.  
  47. <TextView
  48. android:id="@+id/textView2"
  49. android:layout_width="80dp"
  50. android:layout_height="20dp"
  51. android:background="@color/colorPrimary"
  52. android:text="中文显示:"
  53. android:textAlignment="center"
  54. android:textColor="@color/colorWhite"
  55. app:layout_constraintBottom_toBottomOf="parent"
  56. app:layout_constraintHorizontal_bias="0"
  57. app:layout_constraintLeft_toLeftOf="parent"
  58. app:layout_constraintRight_toRightOf="parent"
  59. app:layout_constraintTop_toTopOf="parent"
  60. app:layout_constraintVertical_bias="0.205" />
  61.  
  62. <TextView
  63. android:id="@+id/tv1"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. app:layout_constraintBottom_toBottomOf="parent"
  67. app:layout_constraintHorizontal_bias="0.0"
  68. app:layout_constraintLeft_toLeftOf="parent"
  69. app:layout_constraintRight_toRightOf="parent"
  70. app:layout_constraintTop_toTopOf="parent"
  71. app:layout_constraintVertical_bias="0.244" />
  72.  
  73. <TextView
  74. android:id="@+id/tv2"
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content"
  77. app:layout_constraintBottom_toBottomOf="parent"
  78. app:layout_constraintHorizontal_bias="0.0"
  79. app:layout_constraintLeft_toLeftOf="parent"
  80. app:layout_constraintRight_toRightOf="parent"
  81. app:layout_constraintTop_toTopOf="parent"
  82. app:layout_constraintVertical_bias="0.53" />
  83. </android.support.constraint.ConstraintLayout>

3、activity文件

MainActivity.java

  1. package thonlon.example.cn.sharedpreferencesdemo;
  2.  
  3. import android.content.SharedPreferences;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.  
  13. private SharedPreferences sharedPreferences;
  14. private Button btn_create, btn_getInfo;
  15. private TextView textView1, textView2;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. btn_create = (Button) findViewById(R.id.btn_create);
  22. btn_getInfo = (Button) findViewById(R.id.btn_getInfo);
  23. textView1 = (TextView) findViewById(R.id.tv1);
  24. textView2 = (TextView) findViewById(R.id.tv2);
  25. // 设置配置文件的名称和配置文件的被访问权限
  26. sharedPreferences = getSharedPreferences("config", MODE_ENABLE_WRITE_AHEAD_LOGGING);
  27. btn_create.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View view) {
  30. write();
  31. }
  32.  
  33. private void write() {
  34. //得到配置编辑器
  35. SharedPreferences.Editor edit = sharedPreferences.edit();
  36. //写入配置信息到配置文件中
  37. edit.putString("Chinese", "SharedPreferences是Android平台上一个轻量级的存储类。");
  38. edit.putString("English", "SharedPreferences is a lightweight storage class on the Android platform to save some of the common configuration of the application.");
  39. //注意以上只是将配置信息写入了内存
  40. edit.commit();//提交内存配置信息到本地
  41. Toast.makeText(getApplicationContext(), "成功创建文件", Toast.LENGTH_LONG).show();
  42. }
  43. });
  44. btn_getInfo.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View view) {
  47. read();
  48. }
  49.  
  50. private void read() {
  51. String chinese_info = sharedPreferences.getString("Chinese", "");
  52. String english_info = sharedPreferences.getString("English", "");
  53. textView1.setText(chinese_info);
  54. textView2.setText(english_info);
  55. }
  56. });
  57. }
  58. }

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. 1.面向过程编程 2.面向对象编程 3.类和对象 4.python 创建类和对象 如何使用对象 5.属性的查找顺序 6.初始化函数 7.绑定方法 与非绑定方法

    1.面向过程编程 面向过程:一种编程思想在编写代码时 要时刻想着过程这个两个字过程指的是什么? 解决问题的步骤 流程,即第一步干什么 第二步干什么,其目的是将一个复杂的问题,拆分为若干的小的问题,按照 ...

  2. topcoder srm 445 div1

    problem1 link 这个的结论是只需要考虑坐标是整数或者是整数.5,比如(2.5,3),(4,3.5),(1.5,4.5)这样的时候.这个详细证明起来应该挺麻烦的.这里有一些讨论. probl ...

  3. topcoder srm 699 div1 -3

    1.两个长度为$n$的数组$a,b$,$0 \leq a_{i} <2^{30}$.$b_{i}=-1$或者$b_{i}$为除$a_{i}$外其他数字的抑或值.现在给定$b$,如果不存在$a$, ...

  4. MUSIC分辨率与克拉美罗下界的关系

    https://www.cnblogs.com/rubbninja/p/4512765.html

  5. 集合01_List

    List集合总览 元素有序,可重复,可通过索引访问 增加了通过索引操作集合的方法,如: Object get(int index) Object remove(int index) void sort ...

  6. BZOJ4455 小星星

    闲扯 看到多个限制条件的计数题目,就想到容斥原理 思路 题目要求两个条件 - 编号一一对应 - 树上存在的边,在图上映射到的点上也应该存在 考虑一个暴力的dp,设\(dp_{i,j}\)表示i点编号对 ...

  7. SalGAN: Visual saliency prediction with generative adversarial networks

    SalGAN: Visual saliency prediction with generative adversarial networks 2017-03-17 摘要:本文引入了对抗网络的对抗训练 ...

  8. SQL语句总结2018-11-7

    增加一条数据 insert into table (列字段1,列字段2)values(列1值,列2值) 删除一条数据 delete from table where 列名1=值1 修改一条数据 upd ...

  9. 【ASP.Net】 web api中的media type

    1. 有什么用? 通常用来标识http请求中的内容的类型用来告诉server端如何解析client端发送的message, 或者标识client希望从server端得到的资源是什么样的类型.又被称为M ...

  10. ETCD应用

    etcd:从应用场景到实现原理的全方位解读 ETCD:A highly-available key value store for shared configuration and service d ...