Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中使用多语言项时,如何通过文本区分微调器的选定项?_Android_Android Spinner_Multilingual_Selecteditem_Items - Fatal编程技术网

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

在Android中使用多语言项时,如何通过文本区分微调器的选定项?,android,android-spinner,multilingual,selecteditem,items,Android,Android Spinner,Multilingual,Selecteditem,Items,我试图通过微调器的(多语言)文本来区分所选项目 以下是我的默认strings.xml内容: <string-array name="spinner_items"> <item>length</item> <item>weight</item> <item>temperature</item> </string-array> 然后我添加项目selected

我试图通过微调器的(多语言)文本来区分所选项目

以下是我的默认strings.xml内容:

<string-array name="spinner_items">
    <item>length</item>
    <item>weight</item>
    <item>temperature</item>
</string-array>
然后我添加项目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<*>) {}
}
spinner.onItemSelectedListener=对象:AdapterView.onItemSelectedListener{
覆盖已选择(父项:AdapterView,视图:view,位置:Int,id:Long){
当(spinner.getItemAtPosition(position.toString()){
“长度”->actionLength()
“lunghezza”->actionLength()
“权重”->actionWeight()
“比索”->actionWeight()
“温度”->actionTemperature()
“温度”->actionTemperature()
}
}
override fun on NothingSelected(父级:AdapterView){}
}
一切正常,但问题是每次我添加新的语言区域设置时,我必须记住在when块中添加特定的字符串翻译。
有没有更“动态”的方法来做到这一点?

只需使用
位置即可:

    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<*>) {}
    }
spinner.onItemSelectedListener=对象:AdapterView.onItemSelectedListener{
覆盖已选择(父项:AdapterView,视图:view,位置:Int,id:Long){
何时(职位){
0->actionLength()
1->actionWeight()
2->actionTemperature()
}
}
override fun on NothingSelected(父级:AdapterView){}
}
在阵列中使用:

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

@字符串/长度
@线/重量
@串/温度

我过去也遇到过同样的问题,下面是我解决问题的方法。 通过为数组中的每个项目添加字符串资源名称来编辑strings.xml文件,例如:

Default strings.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>
<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>

我希望我能帮上忙

Lyou不能通过可转换的值查看项目!解决方案-通过这种方式,例如,如果我添加了一种新语言,并在字符串数组中交换了项位置,那么我必须仔细检查所有内容。例如,如果我添加一个带有长度、温度和重量的新字符串数组,这两个值将交换,因此position方法不起作用correctly@Turbo检查更新的答案。只需定义一个具有字符串引用的唯一数组。我喜欢你的答案,但我认为另一个答案“更好”,因为它与职位无关,因为我需要问题中的文本。例如,如果有一天我交换字符串数组中的项目位置,我必须根据我的编辑编辑代码。如果我使用名称而不是位置来检查项目,则每次都是唯一的。我希望你明白我的意思。非常感谢!它很有魅力
<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>
<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>
when(spinner.getItemAtPosition(position).toString()) {
    getString(R.string.length) -> actionLength()
    getString(R.string.weight) -> actionWeight()
    getString(R.string.temperature) -> actionTemperature()
}