我的应用程序中有一个微调器,我想将数组列表的状态保存到其中,因此,如果用户选择一个选项并关闭了该应用程序,我希望微调器在用户重新打开我的应用程序时将其值保存。 / p>
我的主要活动:
t1.typeface = Typeface.createFromAsset(assets, "andlso.ttf")
val fonts = arrayOf("الخط الديواني", "الخط الأندلسي")
spinner.adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fonts)
spinner.onItemSelectedListener = object :AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val SelectedItem = parent!!.getItemAtPosition(position).toString()
val sharedPreferences = getSharedPreferences("savefile", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString("savefile", fonts.toString())
editor.apply()
if (SelectedItem == "الخط الديواني"){
t.typeface = Typeface.createFromAsset(assets, "andlso.ttf")
}else if (SelectedItem == "الخط الأندلسي"){
t.typeface = Typeface.createFromAsset(assets, "frsspbl.TTF")
}
答案 0 :(得分:0)
只需在您的mainActivity中编写以下代码,
val preferences = applicationContext.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)
val prefEditor = preferences.edit()
val fonts = arrayOf("Data1", "Data2", "Data3", "Data4")
var adapterCountry = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fonts)
val spinner = findViewById<Spinner>(R.id.spinner)
spinner.adapter = adapterCountry;
spinner.setSelection(preferences.getInt("position", 0))
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
spinner.setSelection(position)
prefEditor.putInt("position", position)
prefEditor.apply()
}
override fun onNothingSelected(parent: AdapterView<*>) {
}
}
这将完成您的工作。希望对您有帮助。