简体   繁体   English

在 Android 中使用多语言项目时,如何通过文本区分 Spinner 的选定项目?

[英]How can I discriminate the selected item of a Spinner by its text when using multilanguage items in Android?

I am trying to discriminate the selected item of a Spinner by its (multilanguage) text.我试图通过其(多语言)文本来区分 Spinner 的选定项目。

Here is my default strings.xml content:这是我的默认字符串。xml 内容:

<string-array name="spinner_items">
    <item>length</item>
    <item>weight</item>
    <item>temperature</item>
</string-array>

And this is another strings.xml (Italian language) content:而这是另一个strings.xml(意大利语)内容:

<string-array name="spinner_items">
    <item>lunghezza</item>
    <item>peso</item>
    <item>temperatura</item>
</string-array>

I set up my Spinner items in this way:我以这种方式设置我的 Spinner 项目:

val items = resources.getStringArray(R.array.spinner_items)
spinner.adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, items)

And then I add the item selected listener:然后我添加项目选择的侦听器:

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

    override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        when(spinner.getItemAtPosition(position).toString()) {
            "length" -> actionLength()
            "lunghezza" -> actionLength()
            "weight" -> actionWeight()
            "peso" -> actionWeight()
            "temperature" -> actionTemperature()
            "temperatura" -> actionTemperature()
        }
    }

    override fun onNothingSelected(parent: AdapterView<*>) {}
}

Everything works fine but the problem is that everytime I add a new language locale, I have to remember to add the specific string translation inside the when block.一切正常,但问题是每次我添加新的语言区域时,我必须记住在 when 块中添加特定的字符串翻译。 Is there a more "dynamic" way to do this?有没有更“动态”的方式来做到这一点?

I had the same problem in the past and here is how I solved it.我过去遇到过同样的问题,这就是我解决它的方法。 Edit your strings.xml files by adding a string resource name for each items in your array, for example:通过为数组中的每个项目添加字符串资源名称来编辑您的 strings.xml 文件,例如:

Default strings.xml默认字符串。xml

<string name="length">length</string>
<string name="weight">weight</string>
<string name="temperature">temperature</string>

<string-array name="spinner_items">
    <item>@string/length</item>
    <item>@string/weight</item>
    <item>@string/temperature</item>
</string-array>

Italian strings.xml意大利语字符串。xml

<string name="length">lunghezza</string>
<string name="weight">peso</string>
<string name="temperature">temperatura</string>

<string-array name="spinner_items">
    <item>@string/length</item>
    <item>@string/weight</item>
    <item>@string/temperature</item>
</string-array>

So in your code, you'll have:因此,在您的代码中,您将拥有:

when(spinner.getItemAtPosition(position).toString()) {
    getString(R.string.length) -> actionLength()
    getString(R.string.weight) -> actionWeight()
    getString(R.string.temperature) -> actionTemperature()
}

I hope I was helpful!我希望我有帮助!

Just use the position :只需使用position

    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
            when(position) {
              0 -> actionLength()
              1 -> actionWeight()
              2 -> actionTemperature()
            }
        }

        override fun onNothingSelected(parent: AdapterView<*>) {}
    }

In your array use:在您的数组中使用:

<string-array name="spinner_items">
    <item>@string/length</item>
    <item>@string/weight</item>
    <item>@string/temperature</item>
</string-array>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Spinner 再次选中当前选中项时,如何获取事件? - How can I get an event in Android Spinner when the current selected item is selected again? 如何在Android中缩短微调框选择的项目文本? - how to shorten spinner selected item text in android? 当我在android中选择微调项目时,如何获取特定的数组项? - How can i get particular array items when i select the spinner item at android? 如何设置一个微调框的选定项目控制其他微调框的项目? - How can i set one spinner's selected item control other spinner's items? 选择微调器项目后,我希望按钮文本成为选定的微调器项目。 或者我们可以在 string.xml 中做到这一点吗? - When the spinner item is selected, I want the button text to be the selected spinner item. or can we just do it in string.xml? Android Spinner Item选定的文本 - Android Spinner Item selected text 当已在另一个微调器上选择项目时,如何从微调器中删除项目 - How can I remove an item from a spinner when it has been already selected on another spinner Android Espresso:如何匹配自定义微调器选定项目布局上的文本? - Android Espresso: How to match text on custom spinner selected item layout? 如何设置微调框选择的项目以在editTextFocusChange上编辑文本-Android - How to set spinner selected item to edit text on editTextFocusChange- Android 我可以更改Android DatePicker(模式-微调器)所选项目的颜色吗? - Can I change color of Android DatePicker (Mode - Spinner) selected item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM