如何保存按钮的文本(SharedPreferences)

问题描述 投票:-1回答:2

祝你有美好的一天。

我已经使用Android Studio一段时间了,我创建了一个简单的代码来更改按钮的文本,当您单击它时。这是代码:

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
val language = findViewById<Button>(R.id.language)
language.setOnClickListener {
        if ("EN" == language.text) {
            language.text = "IT"
            rollButton.text = "Roll the dice"
            textView.text = "choose the number of dice:"
            if ("Lancia il dado" == Biscotto.text) {
                Biscotto.text = "Roll the dice"
            }
        } else if ("IT" == language.text) {
            language.text = "EN"
            rollButton.text = "Lancia il dado"
            textView.text = "imposta il numero di dadi:"
            if ("Roll the dice" == Biscotto.text) {
                Biscotto.text = "Lancia il dado"
            }
        }
    }

activity_main.xml

<Button
    android:id="@+id/language"
    android:layout_width="74dp"
    android:layout_height="48dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="12dp"
    android:drawableLeft="@drawable/ic_language_black_24dp"
    android:text="EN"
    app:layout_constraintBottom_toTopOf="@+id/divider"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

我如何保存按钮文本,以便在您重新启动应用程序时显示“ IT”或“ EN”,具体取决于它先前获取的最后一个值?

android kotlin sharedpreferences
2个回答
0
投票

未经测试,但这可能有效:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val languageBtn = findViewById<Button>(R.id.language)

        val sharedPref = getSharedPreferences("LANGUAGE", Context.MODE_PRIVATE)
        val editor = sharedPref.edit()
        val currentLocale = sharedPref.getString("LANGUAGE", "EN")

        languageBtn.text = currentLocale

        language.setOnClickListener {
            if (languageBtn.text.contains("EN")) {
                languageBtn.text = "IT"
                editor.putString("LANGUAGE", "IT").commit()
                rollButton.text = "Roll the dice"
                textView.text = "choose the number of dice:"
                if ("Lancia il dado" == Biscotto.text) {
                    Biscotto.text = "Roll the dice"
                }
            } else if (languageBtn.text.contains("IT")) {
                languageBtn.text = "EN"
                editor.putString("LANGUAGE", "EN").commit()
                rollButton.text = "Lancia il dado"
                textView.text = "imposta il numero di dadi:"
                if ("Roll the dice" == Biscotto.text) {
                    Biscotto.text = "Lancia il dado"
                }
            } // else { ... }
        }
    }

在按钮单击事件之外声明您的共享首选项。然后从此处获取存储在首选项中的字符串(“ EN”或“ IT”):然后在按钮文本上设置此值。

此后,在click事件中,您可以将按钮上显示的值与共享首选项中存储的值进行比较,并进行相应的设置。

顺便说一句,不要忘记将所有字符串都放在专用值文件中:)如果您遇到任何麻烦,请告诉我。


0
投票

祝您有美好的一天,我希望这会成功

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val language = findViewById<Button>(R.id.language)
        language.setOnClickListener {
            val sharedPref = getSharedPreferences("LANGUAGE", Context.MODE_PRIVATE)
            val lang = sharedPref.getString("LANGUAGE", "EN")
            if ("EN" == lang) {
                val editor = sharedPref.edit()
                editor.putString("LANGUAGE", "IT")
                editor.commit()
                language.text = "IT"
                rollButton.text = "Roll the dice"
                textView.text = "choose the number of dice:"
                if ("Lancia il dado" == Biscotto.text) {
                    Biscotto.text = "Roll the dice"
                }
            } else if ("IT" == lang) {
                val editor = sharedPref.edit()
                editor.putString("LANGUAGE", "EN")
                editor.commit()
                language.text = "EN"
                rollButton.text = "Lancia il dado"
                textView.text = "imposta il numero di dadi:"
                if ("Roll the dice" == Biscotto.text) {
                    Biscotto.text = "Lancia il dado"
                }
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.