如何获得用户偏好的单位? (公制(米)还是英制(英里))?
我想知道英里和米之间的用户首选单位是什么。似乎“Locale”类不允许这样做。
I would like to know what are the user-preferred units between miles and meters. It seems that the "Locale" class doesn't allow that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管无法使用 Locale 获取首选单位,但您可以默认假设 Locale 指示美国或缅甸 (
locale.getISO3Country().equalsIgnoreCase("usa") || 区域设置。 getISO3Country().equalsIgnoreCase("mmr")
) 首选单位为英制,否则首选单位为公制。在单位可以以英制或公制形式显示的任何情况下,显示的测量系统应该是用户可设置的首选项。某些国家的单位有某些特殊之处。 (另请参阅撰写本文时已接受的答案。)例如,尽管英国首选的日常单位是公制单位,但也有一些例外,特别是
Although there isn't a way to get the preferred units using Locale, you can assume by default that if the Locale indicates the United States or Myanmar (
locale.getISO3Country().equalsIgnoreCase("usa") || locale.getISO3Country().equalsIgnoreCase("mmr")
) the preferred units are imperial, otherwise the preferred units are metric. In any case where units may be shown in either imperial or metric form, the measurement system shown should be a user-settable preference.There are certain idiosyncrasies involving units in some countries. (See also the accepted answer at the time of this writing.) For example, although the preferred everyday units in the United Kingdom are metric units, there are some exceptions, notably—
我认为对于所有语言环境的公制/英制问题不一定有正确的答案。
例如,在英国,我们通常使用英里来表示长途旅行,例如汽车旅行,使用英尺和英寸来表示人的身高,但也可能使用米来描述房间的尺寸。
我们几乎总是使用升来测量液体体积,但啤酒和有时仍然以品脱出售的牛奶除外。
公斤通常比磅和盎司更受欢迎,但大多数食谱书都会列出两者,因为有些人仍然有使用后者的秤。
我认为您必须做一些研究,找出哪些国家/地区使用哪种比例来测量您感兴趣的内容(当然,让用户可以手动选择其中一个或另一个。)
I don't think there's necessarily a correct answer to the Metric/Imperial question for all locales.
For example, in the UK we generally use miles for large distances like a car journey, feet and inches for people's height but may use meters to describe the dimensions of a room.
We almost always use litres for measuring liquid volume except for beer and sometimes milk which can still be sold in pints.
Kilograms are usually preferred to pounds and ounces but most recipe books will list both since some people still have scales which use the latter.
I think you will have to do some research to find out which countries use which scale for what you're interested in measuring (and of course, give the user the option to select one or the other manually.)
由于美国以外的一些人可能已将语言设置为英语(美国),因此最终最好采用
getSimCountryIso()
的返回值,而不是getISO3Country()
如果
countryCode
是us
SIM 卡是美国的,因此英制单位可能是更好的选择 - 在任何其他情况下都应用公制系统Since some people outside the US may have set the language to English (US) it is eventually better to take the return value of
getSimCountryIso()
instead ofgetISO3Country()
If the
countryCode
isus
the SIM card is an american and therefore imperial units might be the better choice - in any other case the metric system is applied构建
区域设置:国家/地区:公制
您可以使用
Locale.getDefault()< /code> 为您正在运行的设备的用户获取适当的区域设置
在构建的表上执行搜索
结果就是你的!
Build a
Locale : Country : Metric
You can use
Locale.getDefault()
to get an appropriate locale for the user of the device you're running onPerform a search on the built table
Result is yours!