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

问题描述 投票:0回答:1

我需要在Android中使用Kotlin保存交换机的位置(实际上是6个交换机)作为用户首选项的一部分。

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

    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()

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

android kotlin sharedpreferences uiswitch
1个回答
1
投票

您的交换机push_switch1和email_switch1都使用相同的Preference 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()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.