如何在Kotlin(android)中使用SharedPreferences获取字符串?

hujrc8aj  于 2023-01-11  发布在  Android
关注(0)|答案(4)|浏览(162)

我只是不知道如何使用这个片段。官方文档显示了这个例子:

val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
val defaultValue = resources.getInteger(R.integer.saved_high_score_default_key)
val highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue)

将“getInt”替换为“getString”始终返回““

val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
    val password = sharedPref?.getString("key", "")
    if (key == "") {
      // this always calls even when I change the value from the settings menu!
    } else {

    }
lmvvr0a8

lmvvr0a81#

这是用于在sharedpreference中存储任何数据类型(字符串、布尔值、整数)的简单类

class PrefUtil(context: Context) {
private val context: Context
val PREFS_NAME = "my_prefs"
fun setInt(key: String?, value: Int) {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    val editor: SharedPreferences.Editor = prefs.edit()
    editor.putInt(key, value)
    editor.apply()
}

fun getInt(key: String?, defValue: Int): Int {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    return prefs.getInt(key, defValue)
}

fun setString(key: String?, value: String?) {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    val editor: SharedPreferences.Editor = prefs.edit()
    editor.putString(key, value)
    editor.apply()
}

fun getString(key: String?): String? {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    return prefs.getString(key, "null")
}

fun setBool(key: String?, value: Boolean) {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    val editor: SharedPreferences.Editor = prefs.edit()
    editor.putBoolean(key, value)
    editor.apply()
}

fun getBool(key: String?): Boolean {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    return prefs.getBoolean(key, false)
}

fun getBool(key: String?, defaultValue: Boolean): Boolean {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    return prefs.getBoolean(key, defaultValue)
} 

init {
    this.context = context
}

}
和用于存储字符串

PrefUtil(this).setString("Key","value")

要从sharedPreference获取字符串,请使用

PrefUtil(this).getString("key")
quhf5bfb

quhf5bfb2#

您需要先为这个键插入一段数据,然后调用get方法:

val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
    with (sharedPref.edit()) {
      putString("key", "abc123456")
      apply()
   } 
    val password = sharedPref?.getString("key", "")
    if (password == "") {
      // ....
      Log.d(TAG,"password is empty.")
    } else {
      Log.d(TAG,">>>:$password")
    }
6ie5vjzr

6ie5vjzr3#

你可以这样试试

//For Add String Value in sharedPreferences
val sharedPreferences = getSharedPreferences("key", MODE_PRIVATE) ?: return
with(sharedPreferences.edit()) {
    putString("yourStringKey", "Hello World")
    apply()
}

 //Here get enter string value from sharedPreferences other activity 
  val sharedPreferences1 = getSharedPreferences("key", MODE_PRIVATE) ?: return
  val string = sharedPreferences1.getString("yourStringKey", "hi") //"hi" is default value
Log.e("sharedPreferences1", "sharedPreferences1 Val is -->> $string")
4jb9z9bj

4jb9z9bj4#

事实证明,我实际上需要使用SharedPreferences,因为我正在使用一个设置活动。谢谢你的回答,因为他们有点帮助引导我需要的方向。

相关问题