Android设置Textview和EditText的语言环境

时间:2017-02-16 16:51:56

标签: android locale

我正在构建一个应用,我明确将应用的区域设置设置为阿拉伯语。但是,我想以英语收集电话号码。 如何将EditText和TextView的语言环境明确设置为USA 英语

以下是我设置应用区域设置的方法。 我有这个助手类

public class LocaleHelper extends ContextThemeWrapper {

    public LocaleHelper(Context base) {
        super(base, R.style.MyTheme);
    }

    @SuppressWarnings("deprecation")
    public static ContextWrapper wrap(Context context, String language) {
        Configuration config = context.getResources().getConfiguration();
        if (!language.equals("")) {
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                setSystemLocale(config, locale);
            } else {
                setSystemLocaleLegacy(config, locale);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                config.setLayoutDirection(locale);
                context = context.createConfigurationContext(config);
            } else {
                context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
            }
        }
        return new LocaleHelper(context);
    }

    @SuppressWarnings("deprecation")
    private static Locale getSystemLocaleLegacy(Configuration config) {
        return config.locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Locale getSystemLocale(Configuration config) {
        return config.getLocales().get(0);
    }

    @SuppressWarnings("deprecation")
    private static void setSystemLocaleLegacy(Configuration config, Locale locale) {
        config.locale = locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static void setSystemLocale(Configuration config, Locale locale) {
        config.setLocale(locale);
    }
}

在我的活动中,我在attachBaseContext中使用此帮助程序,如下所示:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(LocaleHelper.wrap(newBase, "ar"));
}

如何将EditText或TextView的区域设置分别设置为 US

2 个答案:

答案 0 :(得分:1)

您可以String.format()使用Locale.ENGLISH来显示英文数字而不是阿拉伯数字

例如

String txtPrice = String.format(Locale.ENGLISH, "%d", item.getPrice());
mItemPriceTextView.setText(txtPrice);

答案 1 :(得分:0)

问题是应用于EditText的自定义字体没有英文数字,只有阿拉伯语数字,所以无论我尝试了什么,只有阿拉伯语数字会显示出来才有意义。