package com.common;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPHelper {
SharedPreferences sp;
SharedPreferences.Editor editor;
Context context;
public SharedPHelper(Context context,String name) {
super();
this.context = context;
sp=context.getSharedPreferences(name, 0);
editor=sp.edit();
}
public void putValue(String key,String value){
editor=sp.edit();
editor.putString(key, value);
editor.commit();
}
public void putValue(HashMap map){
editor=sp.edit();
for (Map.Entry entry : map.entrySet()) {
editor.putString(entry.getKey(),entry.getValue());
}
editor.commit();
}
public String getValue(String key){
return sp.getString(key, null);
}
public void removeValue(String...strings){
editor=sp.edit();
for (int i=0;i
editor.remove(strings[i]);
}
editor.commit();
}
public void removeValue(HashMap map){
editor=sp.edit();
for (Map.Entry entry : map.entrySet()) {
editor.remove(entry.getKey());
}
editor.commit();
}
}
本文提供了一个使用 Android 中 SharedPreferences 的示例代码,展示了如何保存和读取数据。通过 HashMap 对象批量写入数据,并提供了删除特定键值的方法。

被折叠的 条评论
为什么被折叠?



