Android——SharedPreferences

来源:互联网 发布:mac文本编辑器 编辑:程序博客网 时间:2024/06/05 05:53

SharedPreferences提供一种简单,轻便的数据存取方式,但本身存储的数据类型有限,支持基本类型和String

首先我们要获得一个SharedPreferences对象,可以通过Activity的哟个方法:sp=getPreferences(Mode);Mode=Activity.MODE_PRIVATE;
我们不能直接通过SharedPReferences对象写入数据,而是要获得SharedPreferences对象的editor()对象
Editor editor=sp.edit();

写:
editor.putString(键,值);or putInt(),putBoolean()…

!注意写完之后一定要editor.commit();
!注意写完之后一定要editor.commit();
!注意写完之后一定要editor.commit();

读:
sp.getString(键,默认值);or getInt().,getBoolean()..
默认值指的是如果当前不存在键的时候返回的数据

0 0
原创粉丝点击