当前位置: 代码迷 >> 综合 >> Android--偏好设置(SharedPreferences )
  详细解决方案

Android--偏好设置(SharedPreferences )

热度:60   发布时间:2023-12-14 14:45:17.0
public class StorageUtil {public final static String STORAGE_FILE_NAME = "wangzhan.config";//存数据public static void putString(Context ctx , String key , String value){SharedPreferences sharePreference = getSharePreference(ctx);sharePreference.edit().putString(key, value).commit();}//取数据public static String getString(Context ctx , String key , String... defaultvalue){SharedPreferences sharePreference = getSharePreference(ctx);String dv = "";for (String v : defaultvalue) {dv = v;break;}return sharePreference.getString(key, dv);}private static SharedPreferences getSharePreference(Context ctx){return ctx.getSharedPreferences(STORAGE_FILE_NAME, Context.MODE_PRIVATE);}//存boolean数据public static void putBoolean(Context ctx , String key , boolean value){SharedPreferences sharePreference = getSharePreference(ctx);sharePreference.edit().putBoolean(key, value).commit();}//取boolean数据public static boolean getBoolen(Context ctx , String key , boolean... defaultvalue){SharedPreferences sharePreference = getSharePreference(ctx);boolean dv = false;for (boolean v : defaultvalue) {dv = v;break;}return sharePreference.getBoolean(key, dv);}
}

 

  相关解决方案