【发布时间】:2021-04-02 18:48:13
【问题描述】:
我在登录时使用 EncryptedSharedPreferences 来存储值...我想更改主页活动导航抽屉中的文本
所以当用户登录时它将显示logout 否则它将显示login 导航抽屉
登录活动:--------
sharedPreferences = EncryptedSharedPreferences.create(
"secret_shared_prefs",
masterKeyAlias,
baseContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
) as EncryptedSharedPreferences
登录时我正在这样做
val editor = sharedPreferences.edit()
editor.putString("EmailId", edtEmailId)
editor.putString("password", edtPassword)
editor.apply()
检查 Homeactivity:------
val menu: Menu = bind.navRightView.getMenu()
val nav_connection: MenuItem = menu.findItem(R.id.nav_login)
val sharedPreference =
applicationContext.getSharedPreferences("secret_shared_prefs", Context.MODE_PRIVATE)
val value: String = sharedPreference.getString("EmailId", null).toString()
if(!sharedPreference.contains(value))
{
nav_connection.title = "Loginn"
}
else{
nav_connection.title = "Logout"
}
这段代码的输出总是有一个标题为 loginn,这意味着它检测到的值为 null 需要有关 EncryptedSharedPreferences 的帮助,谢谢
【问题讨论】:
-
你不应该在 contains 方法中使用实际的
value,包含方法期望传递键而不是值作为参数。查看参考:developer.android.com/reference/android/content/… -
你应该试试
sharedPreference.contains('EmailId')。 -
@JeelVankhede 在这里需要帮助 stackoverflow.com/questions/65596957/…
标签: android kotlin sharedpreferences logout encrypted-shared-preference