【发布时间】: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”,具体取决于之前使用的最后一个值?
【问题讨论】:
-
您尝试过使用developer.android.com/reference/android/content/… 吗?从标题看来你知道它的存在。
-
我试图复制粘贴但不知道如何使它工作我马上删除它......
标签: android kotlin sharedpreferences