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

How to save the text of a button (SharedPreferences)

祝你有愉快的一天。

我使用 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",具体取决于它之前采用的最后一个值?

祝你今天愉快,我希望这会奏效

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"
                }
            }
        }
    }

未测试,但这可能有效:

    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"):然后在按钮文本上设置此值。

之后,在点击事件中,您可以将按钮上显示的值与存储在 sharedpreferences 中的值进行比较,并相应地进行设置。

顺便说一句,不要忘记将所有字符串放入专用值文件中:) 如果您遇到问题,请告诉我。