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

最新文章

  1. powershell批量设置权限
  2. 转载:稀疏矩阵存储格式总结+存储效率对比:COO,CSR,DIA,ELL,HYB
  3. 如何用RadioButton做一个底部的切换栏
  4. (转)函数调用方式与extern &quot;C&quot;
  5. App can入门
  6. Java学习文件夹
  7. 一个想法(续三):一份IT技术联盟创业计划书,开启众筹创业征程
  8. Cannot find a valid baseurl for repo: base
  9. Linux下如何进入中文目录
  10. SQL学习指南之查询入门
  11. php中yaf框架的服务器配置
  12. [RESTful] RESTful是什么,为什么要使用它
  13. Node.js的内存问题
  14. LOJ#2170. 「POI2011」木棍 Sticks
  15. Sprint 3.0
  16. 关于Unity3d的Quaternion.LookRotation的学习
  17. [BZOJ4552][TJOI2016&amp;&amp;HEOI2016]排序(二分答案+线段树/线段树分裂与合并)
  18. centos配置jdk的环境变量
  19. nginx在使用proxy_pass的情况下开启error_page
  20. php优化(php.ini)

热门文章

  1. VC++ 判断一个文件是不是快捷方式
  2. Failed to set session cookie. Maybe you are using HTTP instead of HTTPS to access phpMyAdmin.
  3. java利用poi解析excel文件
  4. Navicat for MySQL安装工具及破解工具
  5. C# Math类简介运用
  6. 【C#】C# in deep 笔记
  7. Latex: IEEEtrans模板下 扩大标题宽度
  8. 忘记mysql密码处理方案
  9. SAP-批量修改主数据(客户、供应商、物料)
  10. 常用markdown语法入门