“Android SharedPreferences” Réponses codées

Android SharedPreferences

// load file
SharedPreferences sharedPref = getSharedPreferences(PREFERENCES_FILENAME, MODE_PRIVATE);

// read value
String value = sharedPref.getString(PREFERENCE_KEY, null);

// write value
sharedPref.edit()
  .putString(PREFERENCE_KEY, value)
  .apply();
Merlin4 (ranken)

Android SharedPreferences

SharedPreferences sharedPref = getSharedPreferences("name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("key", "Value");
editor.commit();
Jesus

Obtenir une valeur de préférence Android

boolean mBoolean = PreferenceManager.getDefaultSharedPreferences(yourContext).getBoolean(key, defaultValue); //getBoolean will return defaultValue is key isn't found
//you can also use getInt, getFloat, getLong, getString, getStringSet and change the variable type, of course
Fedeboss

Stockez les données dans SharedPreferences Android

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.apply();
Confused Camel

Définir la valeur de préférence Android

boolean committed = PreferenceManager.getDefaultSharedPreferences(yourContext).edit().putBoolean(key, mValue).commit();
//you can also use putInt, putFloat, putLong, putString, putStringSet and change the value of mValue
//committed is true if everything went alright, false if there was an error
Fedeboss

Réponses similaires à “Android SharedPreferences”

Questions similaires à “Android SharedPreferences”

Plus de réponses similaires à “Android SharedPreferences” dans Java

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code