Android 是否可以获取用户和设备所在的国家/地区的货币代码?

2022-09-03 03:55:32

是否可以获取用户和设备所在的国家/地区的货币代码。我想将用户当前国家/地区的国家/地区代码设置为默认国家/地区。我们在Android中是否有解决方案?


答案 1

由于这段代码可能对您有所帮助,

public class CurrencyTest {
    public static void main(String[] args) throws Exception {
        Locale defaultLocale = Locale.getDefault();
        displayCurrencyInfoForLocale(defaultLocale);

        Locale swedishLocale = new Locale("sv", "SE");
        displayCurrencyInfoForLocale(swedishLocale);
    }

    public static void displayCurrencyInfoForLocale(Locale locale) {
        System.out.println("Locale: " + locale.getDisplayName());
        Currency currency = Currency.getInstance(locale);
        System.out.println("Currency Code: " + currency.getCurrencyCode());
        System.out.println("Symbol: " + currency.getSymbol());
        System.out.println("Default Fraction Digits: " + currency.getDefaultFractionDigits());
        System.out.println();
    }
}

答案 2

获取设备的区域设置,然后检查 http://www.avajava.com/tutorials/lessons/how-do-i-display-the-currency-for-a-locale.html

Locale current = getResources().getConfiguration().locale;
Log.i("locale", Currency.getInstance(current).getCurrencyCode());