SharedPreferences类SharedPreferences.Editor讲解以及数据的存储

Interface for accessing and modifying preference data returned by getSharedPreferences(String, int). For any particular set of preferences,


获得Preferences对象  通过这个方法  可以获得数据 和 修改数据。


there is a single instance of this class that all clients share. 

注意  这个对象是一个单例    我们通过名字来区别。


Modifications to the preferences must go through an SharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage.  

修改的时候  我们需要Editor


getSharedPreferences(String, int)


SharedPreferences.Editor

Interface used for modifying values in a SharedPreferences object. 


SharedPreferences.OnSharedPreferenceChangeListener

Interface definition for a callback to be invoked when a shared preference is changed.




SharedPreferences.Editor

Interface used for modifying values in a SharedPreferences object. All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply()

         public abstract SharedPreferences.Editor clear ()
Added in API level 1

Mark in the editor to remove all values from the preferences. Once commit is called, the only remaining preferences will be any that you have defined in this editor.

Note that when committing back to the preferences, the clear is done first, regardless of whether you called clear before or after put methods on this editor.

Returns
  • Returns a reference to the same Editor object, so you can chain put calls together. 

         public abstract boolean commit ()
Added in API level 1

Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

Note that when two editors are modifying preferences at the same time, the last one to call commit wins.

If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead.

Returns
  • Returns true if the new values were successfully written to persistent storage. 


         public abstract SharedPreferences.Editor putBoolean (String key, boolean value)
Added in API level 1

Set a boolean value in the preferences editor, to be written back once commit() or apply() are called.

Parameters
keyThe name of the preference to modify.
valueThe new value for the preference.
Returns
  • Returns a reference to the same Editor object, so you can chain put calls together. 


         public abstract SharedPreferences.Editor putFloat (String key, float value)
Added in API level 1

Set a float value in the preferences editor, to be written back once commit() or apply() are called.

Parameters
keyThe name of the preference to modify.
valueThe new value for the preference.
Returns
  • Returns a reference to the same Editor object, so you can chain put calls together. 

         public abstract SharedPreferences.Editor putInt (String key, int value)
Added in API level 1

Set an int value in the preferences editor, to be written back once commit() or apply() are called.

Parameters
keyThe name of the preference to modify.
valueThe new value for the preference.
Returns
  • Returns a reference to the same Editor object, so you can chain put calls together. 

         public abstract SharedPreferences.Editor putLong (String key, long value)
Added in API level 1

Set a long value in the preferences editor, to be written back once commit() or apply() are called.

Parameters
keyThe name of the preference to modify.
valueThe new value for the preference.
Returns
  • Returns a reference to the same Editor object, so you can chain put calls together.
public abstract SharedPreferences.Editor putString (String key, String value)
Added in API level 1

Set a String value in the preferences editor, to be written back once commit() or apply() are called.

Parameters
keyThe name of the preference to modify.
valueThe new value for the preference. Supplying null as the value is equivalent to calling remove(String) with this key.
Returns
  • Returns a reference to the same Editor object, so you can chain put calls together. 

         public abstract SharedPreferences.Editor remove (String key)
Added in API level 1

Mark in the editor that a preference value should be removed, which will be done in the actual preferences once commit() is called.

Note that when committing back to the preferences, all removals are done first, regardless of whether you called remove before or after put methods on this editor.

Parameters
keyThe name of the preference to remove.
Returns
  • Returns a reference to the same Editor object, so you can chain put calls together. 



Sharedpreferences

         public abstract boolean contains (String key)
Added in API level 1

Checks whether the preferences contains a preference.

Parameters
keyThe name of the preference to check.
Returns
  • Returns true if the preference exists in the preferences, otherwise false. 

         public abstract SharedPreferences.Editor edit ()
Added in API level 1

Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.

Note that you must call commit() to have any changes you perform in the Editor actually show up in the SharedPreferences.

Returns
  • Returns a new instance of the SharedPreferences.Editor interface, allowing you to modify the values in this SharedPreferences object. 


         public abstract boolean getBoolean (String key, boolean defValue)
Added in API level 1

Retrieve a boolean value from the preferences.

Parameters
keyThe name of the preference to retrieve.
defValueValue to return if this preference does not exist.
Returns
  • Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a boolean.


         public abstract float getFloat (String key, float defValue)
Added in API level 1

Retrieve a float value from the preferences.

Parameters
keyThe name of the preference to retrieve.
defValueValue to return if this preference does not exist.
Returns
  • Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a float.
public abstract int getInt (String key, int defValue)
Added in API level 1

Retrieve an int value from the preferences.

Parameters
keyThe name of the preference to retrieve.
defValueValue to return if this preference does not exist.
Returns
  • Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not an int.


         public abstract long getLong (String key, long defValue)
Added in API level 1

Retrieve a long value from the preferences.

Parameters
keyThe name of the preference to retrieve.
defValueValue to return if this preference does not exist.
Returns
  • Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a long.


         public abstract String getString (String key, String defValue)
Added in API level 1

Retrieve a String value from the preferences.

Parameters
keyThe name of the preference to retrieve.
defValueValue to return if this preference does not exist.
Returns
  • Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String.


注意  这个注册 数据监听器  是注册在SharedPreferences上面的。


         public abstract void registerOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)
Added in API level 1

Registers a callback to be invoked when a change happens to a preference.

Parameters
listenerThe callback that will run.



         public abstract void unregisterOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)
Added in API level 1

Unregisters a previous callback.

Parameters
listenerThe callback that should be unregistered.

取消注册。


例子:


注意  这个地方也给我们一种很好的提示   如果需要使用 Context  上下文的时候  我们可以再函数里面  直接创建一个

Context context  从而就可以直接使用context上下文对象。


public static boolean saveFile(Context context, String username,
String password)


{
// 获取SP对象
SharedPreferences sp = context.getSharedPreferences("info",
Context.MODE_PRIVATE);
// 获取editor对象。
Editor editor = sp.edit();
// 放入数据
editor.putString("username", username);
editor.putString("password", password);
// 提交
editor.commit();


return true;


}
























































































































































































































































































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值