Android SharedPreferences存储用法详解

Android SharedPreferences存储用法详解,第1张

概述先看Demo运行效果SharedPreferences详解SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中

先看Demo运行效果

SharedPreferences详解

SharedPreferences是AndroID平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出.

SharedPreferences提供了java常规的Long、Int、String等类型数据的保存接口.

SharedPreferences类似过去windows系统上的ini配置文件,但是它分为多种权限,可以全局共享访问。

提示最终是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比sqlite要好不少,如果真的存储量不大可以考虑自己定义文件格式。xml处理时Dalvik会通过自带底层的本地XML Parser解析,比如XMLpull方式,这样对于内存资源占用比较好.

SharedPreferences数据的四种 *** 作模式

Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE

Context.MODE_PRIVATE:为默认 *** 作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入

SharedPreferences使用步骤

SharedPreferences的使用非常简单,使用SharedPreferences保存key-value对的步骤如下:

  (1)使用Activity类的getSharedPreferences方法获得SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定.
  (2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象.
  (3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中Xxx表示不同的数据类型。例如:字符串类型的value需要用putString方法.
  (4)通过SharedPreferences.Editor接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit) *** 作.

具体代码的书写流程为:

 A、存放数据信息

1、打开Preferences,名称为config,如果存在则打开它,否则创建新的Preferences
SharedPreferencesconfig= getSharedPreferences(“config”,0);
2、让config处于编辑状态
SharedPreferences.Editor editor =config.edit();
3、存放数据
editor.putString(“name”,”ATAAW”);
editor.putString(“URL”,”ATAAW.COM”);
4、完成提交
editor.commit();

B、读取数据信息
1、获取Preferences
SharedPreferencesconfig= getSharedPreferences(“config”,0);
2、取出数据
String name =config.getString(“name”,”默认值”);
String url = config.getString(“URL”,”default”);
以上就是AndroID中SharedPreferences的使用方法 

demo的实现

MainActivity布局文件

<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:orIEntation="vertical"  tools:context="com.duanlian.sharedpreferencesdemo.MainActivity">   <linearLayout  androID:layout_wIDth="match_parent"  androID:layout_height="50dp">   <TextVIEw   androID:layout_wIDth="wrap_content"   androID:layout_height="match_parent"   androID:gravity="center"   androID:text="用户名"   androID:textSize="23sp" />   <EditText   androID:ID="@+ID/username"   androID:layout_wIDth="0dp"   androID:layout_height="wrap_content"   androID:layout_weight="1" />  </linearLayout>  <linearLayout  androID:layout_wIDth="match_parent"  androID:layout_height="50dp">   <TextVIEw   androID:layout_wIDth="wrap_content"   androID:layout_height="match_parent"   androID:gravity="center"   androID:text="密 码"   androID:textSize="23sp" />   <EditText   androID:ID="@+ID/password"   androID:layout_wIDth="0dp"   androID:layout_height="wrap_content"   androID:layout_weight="1" />  </linearLayout>  <button  androID:onClick="save"  androID:layout_wIDth="match_parent"  androID:layout_height="50dp"  androID:text="保存信息"/>  <button  androID:onClick="change"  androID:layout_wIDth="match_parent"  androID:layout_height="50dp"  androID:text="跳转"/> </linearLayout> 

ActivityA的布局文件

<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:orIEntation="vertical"  tools:context="com.duanlian.sharedpreferencesdemo.MainActivity">   <linearLayout  androID:layout_wIDth="match_parent"  androID:layout_height="50dp">   <TextVIEw   androID:layout_wIDth="wrap_content"   androID:layout_height="match_parent"   androID:gravity="center"   androID:text="用户名"   androID:textSize="23sp" />   <EditText   androID:ID="@+ID/username"   androID:layout_wIDth="0dp"   androID:layout_height="wrap_content"   androID:layout_weight="1" />  </linearLayout>  <linearLayout  androID:layout_wIDth="match_parent"  androID:layout_height="50dp">   <TextVIEw   androID:layout_wIDth="wrap_content"   androID:layout_height="match_parent"   androID:gravity="center"   androID:text="密 码"   androID:textSize="23sp" />   <EditText   androID:ID="@+ID/password"   androID:layout_wIDth="0dp"   androID:layout_height="wrap_content"   androID:layout_weight="1" />  </linearLayout>  <button  androID:onClick="get"  androID:layout_wIDth="match_parent"  androID:layout_height="50dp"  androID:text="得到信息"/> </linearLayout> 

MainActivity里存储的具体实现
都是按照上面写的步骤来实现的,最后一定要提交

package com.duanlian.sharedpreferencesdemo;  import androID.content.Intent; import androID.content.SharedPreferences; import androID.support.v7.app.AppCompatActivity; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.Widget.EditText;  public class MainActivity extends AppCompatActivity {   private EditText passWors;  private EditText username;  private SharedPreferences preferences;  private SharedPreferences.Editor editor;   @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.activity_main);  initVIEw();  }   private voID initVIEw() {  username = (EditText) findVIEwByID(R.ID.username);  passWors = (EditText) findVIEwByID(R.ID.password);  //1,实例化SharedPreferences对象,参数1:保存的名字,参数2:保存的模式MODE_PRIVATE私有的  preferences = getSharedPreferences("config",MODE_PRIVATE);  //2,让SharedPreferences处于可编辑状态  editor = preferences.edit();   }   /**  * 保存按钮的监听  * @param vIEw  */  public voID save(VIEw vIEw) {  //拿到用户输入的数据  String name = username.getText().toString().trim();  String pwd = passWors.getText().toString().trim();  //3,储存数据,类似于map  editor.putString("name",name);  editor.putString("pwd",pwd);  //4,提交  editor.commit();   }   /**  * 跳转的点击监听  * @param vIEw  */  public voID change(VIEw vIEw) {  Intent intent = new Intent(MainActivity.this,ActivityA.class);  startActivity(intent);   } } 

取出数据的实现

package com.duanlian.sharedpreferencesdemo;  import androID.content.SharedPreferences; import androID.support.v7.app.AppCompatActivity; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.Widget.EditText;  public class ActivityA extends AppCompatActivity {   private EditText username;  private EditText passWord;  private String name;  private String pwd;   @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.activity_a);  //得到数据  //1,得到SharedPreferences对象  SharedPreferences preferences = getSharedPreferences("config",0);  //2,取出数据   name = preferences.getString("name","");   pwd = preferences.getString("pwd","");  username = (EditText) findVIEwByID(R.ID.username);  passWord = (EditText) findVIEwByID(R.ID.password);   }   /**  * 得到数据按钮的监听  * @param vIEw  */  public voID get(VIEw vIEw) {  username.setText(name);  passWord.setText(pwd);  } } 

demo下载地址:SharedPreferences

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android SharedPreferences存储用法详解全部内容,希望文章能够帮你解决Android SharedPreferences存储用法详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146925.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存