eclipse 如何获得用户首选的单位?(公制(米)或英制(英里))?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7025998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:49:42  来源:igfitidea点击:

How to get the user preferred units? (metric (meters) or imperial (miles))?

javaandroideclipseunits-of-measurementmetric

提问by Regis_AG

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.

我想知道英里和米之间的用户首选单位是什么。似乎“Locale”类不允许这样做。

采纳答案by Azquelt

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.)

我认为您必须进行一些研究,以找出哪些国家/地区使用哪种比例来衡量您感兴趣的衡量标准(当然,让用户可以手动选择一个或另一个。)

回答by Peter O.

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.

尽管无法使用区域设置获取首选单位,但您可以默认假设,如果区域设置指示美国或缅甸 ( locale.getISO3Country().equalsIgnoreCase("usa") || locale.getISO3Country().equalsIgnoreCase("mmr")),首选单位是英制,否则首选单位是公制。在单位可以以英制或公制形式显示的任何情况下,显示的测量系统应该是用户可设置的首选项。

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—

某些国家的单位存在某些特殊性。(另请参阅撰写本文时已接受的答案。)例如,尽管英国首选的日常单位是公制单位,但也有一些例外,特别是——

  • miles and miles per hour, in ground transportation;
  • miles per hour, in wind speeds;
  • pints and gallons, for milk and beer (but not most other foods and beverages); and
  • stone, or stone and pounds, for a person's weight.
  • 英里和每小时英里,在地面运输中;
  • 英里每小时,风速;
  • 品脱和加仑,用于牛奶和啤酒(但不是大多数其他食品和饮料);和
  • 石头,或石头和磅,对于一个人的体重。

回答by DonGru

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 of getISO3Country()

由于美国以外的一些人可能已将语言设置为英语(美国),因此最终最好采用getSimCountryIso()而不是的返回值getISO3Country()

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();

If the countryCodeis usthe SIM card is an american and therefore imperial units might be the better choice - in any other case the metric system is applied

如果countryCodeusSIM 卡是美国的,因此英制单位可能是更好的选择 - 在任何其他情况下,公制系统被应用

回答by MduSenthil

  1. Build a Locale : Country : Metric

  2. You can use Locale.getDefault()to get an appropriate locale for the user of the device you're running on

  3. Perform a search on the built table

  1. 建个 Locale : Country : Metric

  2. 您可以使用Locale.getDefault()为您正在运行的设备的用户获取适当的语言环境

  3. 在构建的表上执行搜索

Result is yours!

结果是你的!