【发布时间】:2021-12-26 02:36:03
【问题描述】:
我喜欢在共享首选项中保存按钮的颜色。
<Button
android:id="@+id/farbe1"
android:layout_width="20dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:backgroundTint="@color/teal_700"
app:layout_constraintStart_toEndOf="@+id/FachMontag1"
app:layout_constraintTop_toTopOf="@+id/view5" />
按钮具有在按下时改变颜色的能力。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_montag)
val pref = getPreferences(Context.MODE_PRIVATE)
val Fach1 = pref.getString("FACHMONTAG1", "")
val Fach2 = pref.getString("FACHMONTAG2", "")
val Fach3 = pref.getString("FACHMONTAG3", "")
val Fach4 = pref.getString("FACHMONTAG4", "")
val Fach5 = pref.getString("FACHMONTAG5", "")
val Fach6 = pref.getString("FACHMONTAG6", "")
val FachMontag1 = findViewById<EditText>(R.id.FachMontag1)
FachMontag1.setText(Fach1)
val FachMontag2 = findViewById<EditText>(R.id.FachMontag2)
FachMontag2.setText(Fach2)
val FachMontag3 = findViewById<EditText>(R.id.FachMontag3)
FachMontag3.setText(Fach3)
val FachMontag4 = findViewById<EditText>(R.id.FachMontag4)
FachMontag4.setText(Fach4)
val FachMontag5 = findViewById<EditText>(R.id.FachMontag5)
FachMontag5.setText(Fach5)
val FachMontag6 = findViewById<EditText>(R.id.FachMontag6)
FachMontag6.setText(Fach6)
val Farbe1 = findViewById<Button>(R.id.farbe1)
Farbe1.setOnClickListener {
val Farbe1 = findViewById<Button>(R.id.farbe1)
number_of_clicks++
if (number_of_clicks == 1) {
Farbe1.setBackgroundColor(getColor(R.color.white))
number_of_clicks + 1
} else if (number_of_clicks == 2) {
Farbe1.setBackgroundColor(getColor(R.color.yellow))
number_of_clicks + 1
} else if (number_of_clicks == 3) {
Farbe1.setBackgroundColor(getColor(R.color.green))
number_of_clicks + 1
} else if (number_of_clicks == 4) {
Farbe1.setBackgroundColor(getColor(R.color.red))
number_of_clicks + 1
} else if (number_of_clicks == 5) {
Farbe1.setBackgroundColor(getColor(R.color.blue))
number_of_clicks + 1
} else if (number_of_clicks == 6) {
Farbe1.setBackgroundColor(getColor(R.color.purple))
number_of_clicks + 1
} else if (number_of_clicks == 7) {
Farbe1.setBackgroundColor(getColor(R.color.teal_700))
number_of_clicks = 0
}
}
}
我有一个按钮,它还可以保存我的应用程序中的一些其他内容(编辑文本)。
fun onSave(view: android.view.View) {
val pref = getPreferences(Context.MODE_PRIVATE)
val editor = pref.edit()
val FachMontag1 = findViewById<EditText>(R.id.FachMontag1)
editor.putString("FACHMONTAG1", FachMontag1.text.toString())
val FachMontag2 = findViewById<EditText>(R.id.FachMontag2)
editor.putString("FACHMONTAG2", FachMontag2.text.toString())
val FachMontag3 = findViewById<EditText>(R.id.FachMontag3)
editor.putString("FACHMONTAG3", FachMontag3.text.toString())
val FachMontag4 = findViewById<EditText>(R.id.FachMontag4)
editor.putString("FACHMONTAG4", FachMontag4.text.toString())
val FachMontag5 = findViewById<EditText>(R.id.FachMontag5)
editor.putString("FACHMONTAG5", FachMontag5.text.toString())
val FachMontag6 = findViewById<EditText>(R.id.FachMontag5)
editor.putString("FACHMONTAG6", FachMontag6.text.toString())
editor.commit()
}
我想像编辑文本一样保存按钮的颜色,但不知道如何调整颜色的代码。
【问题讨论】:
-
仅供参考,您在保存功能中查找
R.id.FachMontag5两次!重复会导致这类错误,很容易错过
标签: kotlin colors save sharedpreferences background-color