Android 在Kotlin中使用共享首选项检索值
我试图在kotlin中的两个活动之间传递一个值,但是如果我使用下面的代码,那么我只会得到“Hello World”默认值,而不是PREFERENCE_NAME值。我的文本id名称是android:id=“@+id/tv_count”非常感谢您的帮助Android 在Kotlin中使用共享首选项检索值,android,kotlin,Android,Kotlin,我试图在kotlin中的两个活动之间传递一个值,但是如果我使用下面的代码,那么我只会得到“Hello World”默认值,而不是PREFERENCE_NAME值。我的文本id名称是android:id=“@+id/tv_count”非常感谢您的帮助 Main Activity: import android.content.Context import android.support.v7.app.AppCompatActivity import android.os.Bundle import
Main Activity:
import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mypreference=MyPreference(this)
var loginCount=mypreference.getLoginName()
mypreference.setLoginName(loginCount)
tv_count.text=loginCount.toString()
}
}
My Preference:
import android.content.Context
class MyPreference(context:Context)
{
val PREFERENCE_NAME="SharedPreferenceExample"
val preference=context.getSharedPreferences(PREFERENCE_NAME,Context.MODE_PRIVATE)
fun getLoginName():String
{
return preference.getString(PREFERENCE_NAME,"Hello World")
}
fun setLoginName(name:String)
{
val editor=preference.edit()
editor.putString(PREFERENCE_NAME,name)
}
}
您需要调用
commit
fun setLoginName(name:String)
{
val editor=preference.edit()
editor.putString(PREFERENCE_NAME,name)
editor.commit()
}
您需要调用
commit
fun setLoginName(name:String)
{
val editor=preference.edit()
editor.putString(PREFERENCE_NAME,name)
editor.commit()
}
类MyPreference(上下文:上下文)
{
}我的首选项类别(上下文:上下文)
{
}在您的例子中,您没有使用editor.commit()函数。这是全部代码
//Store in SharedPreference
val preference=getSharedPreferences(resources.getString(R.string.app_name), Context.MODE_PRIVATE)
val editor=preference.edit()
editor.putBoolean("isLoggedIn",true)
editor.putInt("id",1)
editor.putString("name","Alex")
editor.commit()
//Retrieve from SharedPreference
val name= preference.getString("name","")
val id= preference.getInt("id",0)
val isLoggedIn= preference.getBoolean("isLoggedIn",false)
在您的例子中,您没有使用editor.commit()函数。这是全部代码
//Store in SharedPreference
val preference=getSharedPreferences(resources.getString(R.string.app_name), Context.MODE_PRIVATE)
val editor=preference.edit()
editor.putBoolean("isLoggedIn",true)
editor.putInt("id",1)
editor.putString("name","Alex")
editor.commit()
//Retrieve from SharedPreference
val name= preference.getString("name","")
val id= preference.getInt("id",0)
val isLoggedIn= preference.getBoolean("isLoggedIn",false)
由于Kotlin中的委托属性,这将是一个更广泛的答案,显示了非常优雅地使用首选项的一般方式。这使我们能够为日常物业提供自己的后备商店 考虑这个描述如何读取和写入布尔值的类:
class BooleanPrefStore(val default: Boolean = false) {
operator fun getValue(thisRef: ContextWrapper?, property: KProperty<*>): Boolean =
PreferenceManager.getDefaultSharedPreferences(thisRef)
.getBoolean(property.name, default)
operator fun setValue(thisRef: ContextWrapper?, property: KProperty<*>, value: Boolean) {
PreferenceManager.getDefaultSharedPreferences(thisRef)
.edit()
.putBoolean(property.name, value)
.apply()
}
}
它甚至允许我们提供一个默认值,如果它不同于“默认值”。如果需要,只需以相同的方式创建其他helpers类,IntPrefStore
、longprestore
或StringPrefStore
。然后,您只需使用这些属性或为它们赋值,所有属性都将自动存储到首选项存储并从中检索
只有一个警告:首选项存储需要访问当前上下文。如果您在保存上下文的活动
、片段
或类似的Android类中声明这些属性,则无需执行其他操作。所有这些类都实现了ContextWrapper
。但如果您需要自己的类中的属性,则需要自己将其设置为ContextWrapper
,例如:
class MyClass private constructor(context: Context) : ContextWrapper(context) {
...
只要在实例化时提供上下文即可。由于Kotlin中的委托属性,这将是一个更广泛的答案,展示了非常优雅地使用首选项的一般方法。这使我们能够为日常物业提供自己的后备商店 考虑这个描述如何读取和写入布尔值的类:
class BooleanPrefStore(val default: Boolean = false) {
operator fun getValue(thisRef: ContextWrapper?, property: KProperty<*>): Boolean =
PreferenceManager.getDefaultSharedPreferences(thisRef)
.getBoolean(property.name, default)
operator fun setValue(thisRef: ContextWrapper?, property: KProperty<*>, value: Boolean) {
PreferenceManager.getDefaultSharedPreferences(thisRef)
.edit()
.putBoolean(property.name, value)
.apply()
}
}
它甚至允许我们提供一个默认值,如果它不同于“默认值”。如果需要,只需以相同的方式创建其他helpers类,IntPrefStore
、longprestore
或StringPrefStore
。然后,您只需使用这些属性或为它们赋值,所有属性都将自动存储到首选项存储并从中检索
只有一个警告:首选项存储需要访问当前上下文。如果您在保存上下文的活动
、片段
或类似的Android类中声明这些属性,则无需执行其他操作。所有这些类都实现了ContextWrapper
。但如果您需要自己的类中的属性,则需要自己将其设置为ContextWrapper
,例如:
class MyClass private constructor(context: Context) : ContextWrapper(context) {
...
实例化时只需提供上下文即可。感谢John O'Reilly的回复。不过我还是在屏幕上看到了Hello World谢谢John O'Reilly的回复。我仍然在屏幕上看到Hello World谢谢你的建议!谢谢你的建议!在科特林,我们可以使用不同的方法。查看此处了解更多详细信息:在kotlin中,我们可以使用不同的方法。请查看此处了解更多详细信息: