猿问

针对可翻译字符串执行条件语句

我的应用程序中有一个微调器,它显示了几个距离单位,它们是从中提取的strings.xml(看起来像这样):


<string-array name="distance_units_array">

        <item>miles</item>

        <item>kilometers</item>

        <item>meters</item>

        <item>yards</item>

    </string-array>

如果我需要对用户输入的距离进行一些计算,但是执行的计算会因距离单位而异。我可以使用以下方法来做到这一点,但这是一个可怕的 hack 解决方案,使用多种语言会变得非常痛苦。


if (distanceUnit = “kilometres” || distanceUnit = “kilómetros” || etc){

    //Do stuff

}

if (distanceUnit = “miles” || distanceUnit = “millas” || etc){

    //Do other stuff

}

关于更好实施的任何建议?我觉得每个人都distanceUnit需要一个 ID,这样我就可以与其进行比较而不是翻译它,但我不确定当字符串存储在strings.xml.


慕田峪4524236
浏览 99回答 1
1回答

MMMHUHU

您应该开始比较他们的 ID,而不是他们的内容。yourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switch(position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 0 : //calculate miles&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 1 : //calculate kilometres&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.....&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onNothingSelected(AdapterView<?> arg0) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

Java
我要回答