【问题标题】:How to save the text of a button (SharedPreferences)如何保存按钮的文本(SharedPreferences)
【发布时间】:2020-03-11 14:50:47
【问题描述】:

祝你有美好的一天。

我使用 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


【解决方案1】:

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

    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 事件中,您可以将按钮上显示的值与 sharedpreferences 中存储的值进行比较,并进行相应的设置。

顺便说一句,不要忘记将所有字符串放在专用值文件中 :) 让我知道如果你有什么麻烦。

【讨论】:

    【解决方案2】:

    祝你有美好的一天,我希望这会奏效

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

    【讨论】:

    • 谢谢,我试试
    • 没关系,这样就解决了问题,但是记得不要把这段代码放到onclicklistener里,更不要放到activity代码里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 2012-05-29
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多