android - 如何使用 Kotlin 在 Android 中保存开关设置

标签 android kotlin sharedpreferences uiswitch

我需要使用 Kotlin 将开关的位置(实际上是 6 个开关)保存为 Android 中用户首选项的一部分。

我用 Java 编写了代码,没有任何问题,但该代码需要用 Kotlin 编写。 我正在考虑像 Java 一样使用共享首选项,并成功生成了代码来保存一个开关的状态。但是,当我编写代码来添加第二个开关时,第一个开关会控制其他开关,并且它们的状态保存为与第一个开关相同。此外,所有后续切换都将重现相同的情况。 我尝试过 Kotlin.org 代码转换器/翻译器,但这会产生一堆乱码,我必须在编译之前清理这些乱码,然后发现翻译后的代码可能不完整。

    private fun onSwitch() {

    val sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE)
    val editor = sharedPreferences.edit()


    push_switch1.isChecked = sharedPreferences.getBoolean(PREF_SWITCH, false)
    push_switch1.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked){
            editor.putBoolean(PREF_SWITCH, push_switch1.isChecked)
            editor.putBoolean(PREF_SWITCH, true)
            Toast.makeText(this@MainActivity, "Push Notification ON", Toast.LENGTH_SHORT).show()
        } else {
            editor.putBoolean(PREF_SWITCH, false)
            Toast.makeText(this@MainActivity, "Push Notification Off", Toast.LENGTH_SHORT).show()
        }
        //editor.apply()
    }
    email_switch1.isChecked = sharedPreferences.getBoolean(PREF_SWITCH, false)
    email_switch1.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked){
            editor.putBoolean(PREF_SWITCH, email_switch1.isChecked)
            editor.putBoolean(PREF_SWITCH, true)
            Toast.makeText(this@MainActivity, "Email Notification ON", Toast.LENGTH_SHORT).show()
        }else{
            editor.putBoolean(PREF_SWITCH, false)
            Toast.makeText(this@MainActivity, "Email Notification OFF", Toast.LENGTH_SHORT).show()
        }
        //editor.apply()
    }
    editor.apply()

这是一个首选项页面,每个开/关开关都会打开或关闭特定的用户首选项。此外,开关状态需要持续保存以保留用户的设置。

最佳答案

您的开关 Push_switch1 和 email_switch1 使用相同的首选项 key ,即 PREF_SWITCH。

您需要为每个交换机添加唯一的首选项 key 。 添加 PREF_SWITCH_PUSH 和 PREF_SWITCH_EMAIL 首选项。 那么试试这个...

private fun onSwitch() {

    val sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE)
    val editor = sharedPreferences.edit()


    push_switch1.isChecked = sharedPreferences.getBoolean(PREF_SWITCH_PUSH, false)
    push_switch1.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked){
            editor.putBoolean(PREF_SWITCH_PUSH, true)
            Toast.makeText(this@MainActivity, "Push Notification ON", Toast.LENGTH_SHORT).show()
        } else {
            editor.putBoolean(PREF_SWITCH_PUSH, false)
            Toast.makeText(this@MainActivity, "Push Notification Off", Toast.LENGTH_SHORT).show()
        }
        editor.apply()
    }
    email_switch1.isChecked = sharedPreferences.getBoolean(PREF_SWITCH_EMAIL, false)
    email_switch1.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked){
            editor.putBoolean(PREF_SWITCH_EMAIL, true)
            Toast.makeText(this@MainActivity, "Email Notification ON", Toast.LENGTH_SHORT).show()
        }else{
            editor.putBoolean(PREF_SWITCH_EMAIL, false)
            Toast.makeText(this@MainActivity, "Email Notification OFF", Toast.LENGTH_SHORT).show()
        }
        editor.apply()
    }
}

关于android - 如何使用 Kotlin 在 Android 中保存开关设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54700392/

相关文章:

android - 用于蓝牙设备发现的 BroadcastReceiver 适用于一台设备,但不适用于另一台设备

android - ListView 项目在 android 中重复?

android - 如何解决致命异常 : DefaultDispatcher-worker-4

kotlin - Hilt Activity必须附加到@AndroidEntryPoint应用程序

android - SharedPreferences 值未更新

android - 如何在android中获取字符串的像素宽度?

java - 使用 controlTransfer 与 USB 设备通信

android - 使用导航组件时如何设置对话框的目标 fragment

android - 何时使用多个首选项文件?

xamarin - Xamarin.forms 中的共享首选项