如何使用 Spinner 更改变量的值?

How to change values of variable using Spinner?

如何使用微调器小部件更改变量的值。

strings.xml

    <string name="selected_item">Selected item:</string>
    <string-array name="Languages">
        <item>English</item>
        <item>Latin</item>
        <item>French</item>
    </string-array>

主要活动

        val spinner = findViewById<Spinner>(R.id.spinner)
        if (spinner != null) {
            val adapter = ArrayAdapter(
                this,
                android.R.layout.simple_spinner_item, languages
            )
            spinner.adapter = adapter

            spinner.onItemSelectedListener = object :
                AdapterView.OnItemSelectedListener {
                override fun onItemSelected(
                    parent: AdapterView<*>,
                    view: View, position: Int, id: Long
                ) {
                  
                   //what to do here?
                }

            }
        }

假设我想改变这个变量的值

strokeManager.lang = "en"

因此您将希望使用该位置从用于填充微调器的列表中访问它。

strokeManager.lang = languageMapToLangCode[languages.get(position)] 

这应该在您的 class 微调器可以访问的地方定义

val languageMapToLangCode: HashMap<String, String> = hashMapOf("French" to "fr", "Latin" to "ln", "English to "en")