android.content.SharedPreferences.getFloat()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(207)

本文整理了Java中android.content.SharedPreferences.getFloat()方法的一些代码示例,展示了SharedPreferences.getFloat()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SharedPreferences.getFloat()方法的具体详情如下:
包路径:android.content.SharedPreferences
类名称:SharedPreferences
方法名:getFloat

SharedPreferences.getFloat介绍

[英]Retrieve a float value from the preferences.
[中]从首选项中检索浮点值。

代码示例

代码示例来源:origin: libgdx/libgdx

@Override
public float getFloat (String key) {
  return sharedPrefs.getFloat(key, 0);
}

代码示例来源:origin: libgdx/libgdx

@Override
public float getFloat (String key, float defValue) {
  return sharedPrefs.getFloat(key, defValue);
}

代码示例来源:origin: libgdx/libgdx

@Override
public float getFloat (String key, float defValue) {
  return sharedPrefs.getFloat(key, defValue);
}

代码示例来源:origin: libgdx/libgdx

@Override
public float getFloat (String key) {
  return sharedPrefs.getFloat(key, 0);
}

代码示例来源:origin: smuyyh/BookReader

public float getFloat(String key, float defaultVal) {
  return this.prefs.getFloat(key, defaultVal);
}

代码示例来源:origin: smuyyh/BookReader

public float getFloat(String key) {
  return this.prefs.getFloat(key, 0f);
}

代码示例来源:origin: yanzhenjie/NoHttp

public float getFloat(String key, float defaultValue) {
  return mPreferences.getFloat(key, defaultValue);
}

代码示例来源:origin: androidannotations/androidannotations

@Override
public Float getOr(Float defaultValue) {
  try {
    return sharedPreferences.getFloat(key, defaultValue);
  } catch (ClassCastException e) {
    // The pref could be a String, if that is the case try this
    // recovery bit
    try {
      String value = sharedPreferences.getString(key, "" + defaultValue);
      return Float.parseFloat(value);
    } catch (Exception e2) {
      // our recovery bit failed. The problem is elsewhere. Send the
      // original error
      throw e;
    }
  }
}

代码示例来源:origin: jiangqqlmj/FastDev4Android

public float getFloatValue(String key) {
  if (key != null && !key.equals("")) {
    return sp.getFloat(key, 0);
  }
  return 0;
}

代码示例来源:origin: seven332/EhViewer

public static float getFloat(String key, float defValue) {
  try {
    return sSettingsPre.getFloat(key, defValue);
  } catch (ClassCastException e) {
    Log.d(TAG, "Get ClassCastException when get " + key + " value", e);
    return defValue;
  }
}

代码示例来源:origin: Rukey7/MvpApp

/**
 * get float preferences
 * 
 * @param context
 * @param key The name of the preference to retrieve
 * @param defaultValue Value to return if this preference does not exist
 * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
 *         this name that is not a float
 */
public static float getFloat(Context context, String key, float defaultValue) {
  return PreferenceManager.getDefaultSharedPreferences(context).getFloat(key, defaultValue);
}

代码示例来源:origin: Rukey7/MvpApp

/**
 * get float preferences
 * 
 * @param context
 * @param key The name of the preference to retrieve
 * @param defaultValue Value to return if this preference does not exist
 * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
 *         this name that is not a float
 */
public static float getFloat(Context context, String key, float defaultValue) {
  SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
  return settings.getFloat(key, defaultValue);
}

代码示例来源:origin: TeamNewPipe/NewPipe

private static float getScreenBrightness(@NonNull final Context context, final float screenBrightness) {
  SharedPreferences sp = getPreferences(context);
  long timestamp = sp.getLong(context.getString(R.string.screen_brightness_timestamp_key), 0);
  // hypothesis: 4h covers a viewing block, eg evening. External lightning conditions will change in the next
  // viewing block so we fall back to the default brightness
  if ((System.currentTimeMillis() - timestamp) > TimeUnit.HOURS.toMillis(4)) {
    return screenBrightness;
  } else {
    return sp.getFloat(context.getString(R.string.screen_brightness_key), screenBrightness);
  }
}

代码示例来源:origin: jaydenxiao2016/AndroidFire

public static Float getSharedFloatData(Context context, String key) {
  if (sp == null) {
    init(context);
  }
  return sp.getFloat(key, 0f);
}

代码示例来源:origin: HotBitmapGG/bilibili-android-client

public static float getFloat(String key, float defValue) {
  return PreferenceManager.getDefaultSharedPreferences(BilibiliApp.getInstance()).getFloat(key, defValue);
}

代码示例来源:origin: hidroh/materialistic

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
private void restorePosition() {
  setX(getPreferences().getFloat(mPreferenceX, getX()));
  setY(getPreferences().getFloat(mPreferenceY, getY()));
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnDefaultValues() throws Exception {
 SharedPreferences anotherSharedPreferences = context.getSharedPreferences("bazBang", Context.MODE_PRIVATE);
 assertFalse(anotherSharedPreferences.getBoolean("boolean", false));
 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(666f);
 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(666);
 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(666L);
 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("wacka wa");
}

代码示例来源:origin: robolectric/robolectric

@Test
public void commit_shouldStoreValues() throws Exception {
 editor.commit();
 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
 assertTrue(anotherSharedPreferences.getBoolean("boolean", false));
 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f);
 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2);
 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L);
 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar");
 assertThat(anotherSharedPreferences.getStringSet("stringSet", null)).isEqualTo(stringSet);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void commit_shouldRemoveValuesThenSetValues() throws Exception {
 editor.putString("deleteMe", "foo").commit();
 editor.remove("deleteMe");
 editor.putString("dontDeleteMe", "baz");
 editor.remove("dontDeleteMe");
 editor.commit();
 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
 assertThat(anotherSharedPreferences.getBoolean("boolean", false)).isTrue();
 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f);
 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2);
 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L);
 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar");
 assertThat(anotherSharedPreferences.getString("deleteMe", "awol")).isEqualTo("awol");
 assertThat(anotherSharedPreferences.getString("dontDeleteMe", "oops")).isEqualTo("oops");
}

代码示例来源:origin: robolectric/robolectric

@Test
public void commit_shouldClearThenSetValues() throws Exception {
 editor.putString("deleteMe", "foo");
 editor.clear();
 editor.putString("dontDeleteMe", "baz");
 editor.commit();
 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
 assertTrue(anotherSharedPreferences.getBoolean("boolean", false));
 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f);
 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2);
 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L);
 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar");
 // Android always calls clear before put on any open editor, so here "foo" is preserved rather than cleared.
 assertThat(anotherSharedPreferences.getString("deleteMe", "awol")).isEqualTo("foo");
 assertThat(anotherSharedPreferences.getString("dontDeleteMe", "oops")).isEqualTo("baz");
}

相关文章