下面列出了android.content.SharedPreferences.Editor#putFloat ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 保存数据到文件
*
* @param context
* @param key
* @param data
*/
public static void saveData(Context context, String key, Object data) {
SharedPreferences sharedPreferences = context
.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
if (data instanceof Integer) {
editor.putInt(key, (Integer) data);
} else if (data instanceof Boolean) {
editor.putBoolean(key, (Boolean) data);
} else if (data instanceof String) {
editor.putString(key, (String) data);
} else if (data instanceof Float) {
editor.putFloat(key, (Float) data);
} else if(data instanceof Double) {
editor.putFloat(key, Float.valueOf(data + ""));
}else if (data instanceof Long) {
editor.putLong(key, (Long) data);
}
editor.apply();
}
@SuppressLint("NewApi")
private void editorPutValues(Editor editor, String preferKey, Object valueData) {
if (editor == null) {
return;
}
if (valueData == null) {
editor.putString(preferKey, null);
} else {
if (valueData instanceof String) {
editor.putString(preferKey, (String) valueData);
} else if (valueData instanceof Integer) {
editor.putInt(preferKey, (Integer) valueData);
} else if (valueData instanceof Boolean) {
editor.putBoolean(preferKey, (Boolean) valueData);
} else if (valueData instanceof Float) {
editor.putFloat(preferKey, (Float) valueData);
} else if (valueData instanceof Set) {
if (Util.isCompateApi(11))
editor.putStringSet(preferKey, (Set<String>) valueData);
} else if (valueData instanceof Long) {
editor.putLong(preferKey, (Long) valueData);
}
}
}
private static void a(String str, Object obj) {
Editor edit = c.edit();
if (obj.getClass() == Boolean.class) {
edit.putBoolean(str, ((Boolean) obj).booleanValue());
}
if (obj.getClass() == String.class) {
edit.putString(str, (String) obj);
}
if (obj.getClass() == Integer.class) {
edit.putInt(str, ((Integer) obj).intValue());
}
if (obj.getClass() == Float.class) {
edit.putFloat(str, (float) ((Float) obj).intValue());
}
if (obj.getClass() == Long.class) {
edit.putLong(str, (long) ((Long) obj).intValue());
}
edit.commit();
}
@Override
public void onStop() {
super.onStop();
// Store threshold values
Editor editor = prefs.edit();
editor.putFloat("faceThreshold", faceThreshold);
editor.putFloat("distanceThreshold", distanceThreshold);
editor.putInt("maximumImages", maximumImages);
editor.putBoolean("useEigenfaces", useEigenfaces);
editor.putInt("mCameraIndex", mOpenCvCameraView.mCameraIndex);
editor.apply();
// Store ArrayLists containing the images and labels
if (images != null && imagesLabels != null) {
tinydb.putListMat("images", images);
tinydb.putListString("imagesLabels", imagesLabels);
}
}
/**
*
* @author zhoufeng
* @createtime 2015-3-30 下午2:59:33
* @Decription 向SharedPreferences添加信息
*
* @param context 上下文
* @param SharedPreferName SharedPreferences的名称
* @param type 数据的类型
* @param key 数据的名称
* @param value 数据的值
*/
public static void saveSharedPreferInfo(Context context, String SharedPreferName, String type, String key,
Object value) {
SharedPreferences userPreferences;
userPreferences = context.getSharedPreferences(SharedPreferName, Context.MODE_PRIVATE);
Editor editor = userPreferences.edit();
if ("String".equals(type)) {
editor.putString(key, (String) value);
} else if ("Integer".equals(type)) {
editor.putInt(key, (Integer) value);
} else if ("Boolean".equals(type)) {
editor.putBoolean(key, (Boolean) value);
} else if ("Float".equals(type)) {
editor.putFloat(key, (Float) value);
} else if ("Long".equals(type)) {
editor.putLong(key, (Long) value);
}
editor.commit();
}
/**
* Save the display size of Activity.
*
* @param activity an activity
*/
public static void saveDisplaySize(Activity activity) {
DisplayMetrics displaymetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay()
.getMetrics(displaymetrics);
Editor editor = getPreferences().edit();
editor.putInt("screen_width", displaymetrics.widthPixels);
editor.putInt("screen_height", displaymetrics.heightPixels);
editor.putFloat("density", displaymetrics.density);
apply(editor);
TLog.log("", "Resolution:" + displaymetrics.widthPixels + " x "
+ displaymetrics.heightPixels + " DisplayMetrics:" + displaymetrics.density
+ " " + displaymetrics.densityDpi);
}
/**
* convenience method for setting the given value to this {@link SettingsObject}
* and also saving the value in the apps' settings.<br><br/>
* VERY IMPORTANT!!! <br><br/>
* this method does NOT perform validity checks on the given "value" and
* assumes that it is a valid value to be saved as a setting!
* for example, if this method is called from {@link SeekBarSettingsObject},
* it is assumed that "value" is between {@link SeekBarSettingsObject#getMinValue()}
* and {@link SeekBarSettingsObject#getMaxValue()}
* @param context the context to be used to get the app settings
* @param value the VALID value to be saved in the apps' settings
* @throws IllegalArgumentException if the given value is of a type which cannot be saved
* to SharedPreferences
*/
public void setValueAndSaveSetting(Context context, V value)
throws IllegalArgumentException
{
setValue(value);
Editor editor = EasySettings.retrieveSettingsSharedPrefs(context).edit();
switch (getType())
{
case VOID:
//no actual value to save
break;
case BOOLEAN:
editor.putBoolean(getKey(), (Boolean) value);
break;
case FLOAT:
editor.putFloat(getKey(), (Float) value);
break;
case INTEGER:
editor.putInt(getKey(), (Integer) value);
break;
case LONG:
editor.putLong(getKey(), (Long) value);
break;
case STRING:
editor.putString(getKey(), (String) value);
break;
case STRING_SET:
editor.putStringSet(getKey(), getStringSetToSave((Set<?>) value));
break;
default:
throw new IllegalArgumentException("parameter \"value\" must be of a type that " +
"can be saved in SharedPreferences. given type was "
+ value.getClass().getName());
}
editor.apply();
}
/**
* @param key The name of the preference to modify.
* @param value The new value for the preference.
* @see Editor#putFloat(String, float)
*/
public static void putFloat(final String key, final float value) {
final Editor editor = getPreferences().edit();
editor.putFloat(key, value);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
editor.commit();
} else {
editor.apply();
}
}
/**
* 保存Float数据到配置文件
*/
public static Boolean putFloat(String preName, Context context, String key, float value) {
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
Editor editor = pre.edit();
editor.putFloat(key, value);
return editor.commit();
}
/**
* Set a preference float value
* @param key the preference key to set
* @param value the value for this key
*/
public void setPreferenceFloatValue(String key, float value) {
if(sharedEditor == null) {
Editor editor = prefs.edit();
editor.putFloat(key, value);
editor.commit();
}else {
sharedEditor.putFloat(key, value);
}
}
/**
* Put float data to shared preferences in private mode.
* @param context - The context of activity which is requesting to put data.
* @param key - Used to identify the value.
* @param value - The actual value to be saved.
*/
public static void putFloat(Context context, String key, float value) {
SharedPreferences mainPref =
context.getSharedPreferences(context.getResources()
.getString(R.string.shared_pref_package),
Context.MODE_PRIVATE
);
Editor editor = mainPref.edit();
editor.putFloat(key, value);
editor.commit();
}
/**
* 存储一条数据到首选项文件中
* @param preferFileName 要保存的SharedPreference 文件名
* @param key
* @param value
*/
@SuppressLint("NewApi")
public boolean saveValue(String preferFileName, String key, Object value) {
if (Util.isEmpty(preferFileName)) {
return false;
}
Editor mEditor = getSpByName(preferFileName).edit();
if (value == null) {
mEditor.putString(key, null);
}
else{
String valueType = value.getClass().getSimpleName();
if ("String".equals(valueType)) {
mEditor.putString(key, (String) value);
} else if ("Integer".equals(valueType)) {
mEditor.putInt(key, (Integer) value);
} else if ("Boolean".equals(valueType)) {
mEditor.putBoolean(key, (Boolean) value);
} else if ("Float".equals(valueType)) {
mEditor.putFloat(key, (Float) value);
} else if ("Long".equals(valueType)) {
mEditor.putLong(key, (Long) value);
} else if ("Set".equals(valueType)) {
if (Util.isCompateApi(11)) {
mEditor.putStringSet(key, (Set<String>) value);
}
}
}
return mEditor.commit();
}
/**
* Sets a float preference value.
*
* @param context the context
* @param keyId the key id
* @param value the value
*/
@SuppressLint("CommitPrefEdits")
public static void setFloat(Context context, int keyId, float value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putFloat(getKey(context, keyId), value);
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
}
public void testExportImportPreferences() throws Exception {
// Populate with some initial values
Editor editor = preferences.edit();
editor.clear();
editor.putBoolean("bool1", true);
editor.putBoolean("bool2", false);
editor.putFloat("flt1", 3.14f);
editor.putInt("int1", 42);
editor.putLong("long1", 123456789L);
editor.putString("str1", "lolcat");
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
// Export it
byte[] exported = preferenceBackupHelper.exportPreferences(preferences);
// Mess with the previous values
editor = preferences.edit();
editor.clear();
editor.putString("str2", "Shouldn't be there after restore");
editor.putBoolean("bool2", true);
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
// Import it back
preferenceBackupHelper.importPreferences(exported, preferences);
assertFalse(preferences.contains("str2"));
assertTrue(preferences.getBoolean("bool1", false));
assertFalse(preferences.getBoolean("bool2", true));
assertEquals(3.14f, preferences.getFloat("flt1", 0.0f));
assertEquals(42, preferences.getInt("int1", 0));
assertEquals(123456789L, preferences.getLong("long1", 0));
assertEquals("lolcat", preferences.getString("str1", ""));
}
public void setFloatPreference(String key, float value) {
Editor ed = preference.edit();
ed.putFloat(key, value);
ed.apply();
}
public void setFloatPreference(String key, float value) {
Editor ed = preference.edit();
ed.putFloat(key, value);
ed.apply();
}
public static boolean putFloatTemp(String key, float value)
{
Editor editor = editTemp();
editor.putFloat(key, value);
return editor.commit();
}
public static boolean putFloat(String key, float value)
{
Editor editor = edit();
editor.putFloat(key, value);
return editor.commit();
}
/**
* 批量存储数据 参数的长度一定要一致 并且keys中的key 与 values中同序号的元素要一一对应,以保证数据存储的正确性
* @param preferFileName
* @param keys
* @param vTypes keys中键key要存储的对应的 values中的元素的数据类型 eg.: keys[0]= "name"; vTypes[0] = V_TYPE_STR;values[0]="ZhangSan"
* @param values 要存储的值
*/
@Deprecated
@SuppressLint("NewApi")
public boolean saveValue(String preferFileName, String[] keys, byte[] vTypes, Object... values) {
if (keys == null || vTypes == null || values == null) {
return false;
}
int keysLen = keys.length;
if (keysLen == 0)
return false;
int vTypesLen = vTypes.length;
int valuesLen = values.length;
if (vTypesLen == 0 || valuesLen == 0) {
return false;
}
//三者长度取最小的,以保证任何一个数组中不出现异常
int canOptLen = Math.min(keysLen, Math.min(vTypesLen,valuesLen));
Editor mEditor = getSpByName(preferFileName).edit();
for (int i = 0; i < canOptLen; i++) {
byte vType = vTypes[i];
String key = keys[i];
Object v = values[i];
switch (vType) {
case V_TYPE_STR:
mEditor.putString(key, (String) v);
break;
case V_TYPE_INT:
mEditor.putInt(key, (Integer) v);
break;
case V_TYPE_BOOL:
mEditor.putBoolean(key, (Boolean) v);
break;
case V_TYPE_LONG:
mEditor.putLong(key, (Long) v);
break;
case V_TYPE_FLOAT:
mEditor.putFloat(key, (Float) v);
break;
case V_TYPE_SET_STR:
if (Util.isCompateApi(11)) {
mEditor.putStringSet(key, (Set<String>) v);
}
break;
}
}
return mEditor.commit();
}
/**
* 设置Float类型值
*
* @param key
* @param value
*/
public void putFloat(String key, float value) {
Editor editor = sharedPreferences.edit();
editor.putFloat(key, value);
editor.commit();
}