
SharedPerference不同同于文件存储,它是使用键值的方式来存储数据,对于保存的每一条数据都会给一个键值,这样在读取数据时直接通过键值取出相应数据。amdroID提供了三个方法来获取实例:
1.Context类中的getSharePreferences()方法
它接收两个参数,第一个是文件名;第二个是 *** 作模式,目前只有MODE_PRIVATE可选,这是默认的 *** 作模式,表示只有当前的应用可以对文件进行 *** 作。
2.Activity类中的getPreference()方法
它只接收一个 *** 作模式参数,因为使用这个方法会自动将类名SharedPreference作为文件名。
3.PreferenceManager类中的getDefaultSharedPreference()方法
主要由三步来实现:
(1)调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象。
(2)向SharedPreferences.Editor对象中添加数据,比如添加一个布尔型数据就使用putBoolean()方法,依次论推。
(3)调用apply()方法将添加的数据提交,从而完成数据 *** 作。`
使用案例
MainActivity:
package com.example.sharedpreferences;import androID.content.SharedPreferences;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener{ private button button; private button restore_btn; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); button = (button)findVIEwByID(R.ID.save_data); button.setonClickListener(this); restore_btn = (button)findVIEwByID(R.ID.restore_data); restore_btn.setonClickListener(this); } @OverrIDe public voID onClick(VIEw vIEw) { switch (vIEw.getID()){ case R.ID.save_data: saveData(); Toast.makeText(MainActivity.this,"保存成功!",Toast.LENGTH_SHORT).show(); break; case R.ID.restore_data: restorData(); Toast.makeText(MainActivity.this,"恢复成功",Toast.LENGTH_SHORT).show(); break; default: break; } } public voID saveData(){ SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit(); editor.putString("name","Tom"); editor.putInt("age",18); editor.putBoolean("marrIEd",false); editor.apply(); } public voID restorData(){ SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE); String name = preferences.getString("name",""); int age = preferences.getInt("age",0); boolean marrIEd = preferences.getBoolean("marred",false); Log.d("主活动: ","获取到名字: "+name); Log.d("主活动: ","获取到年龄: "+age); Log.d("主活动: ","获取到婚配: "+marrIEd); }}布局文件
<?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="com.example.sharedpreferences.MainActivity"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical"> <button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/save_data" androID:text="保存数据"/> <button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/restore_data" androID:text="恢复数据"/> </linearLayout></androID.support.constraint.ConstraintLayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android学习之SharedPerference存储详解全部内容,希望文章能够帮你解决Android学习之SharedPerference存储详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)