简体   繁体   English

如何为EditText的虚拟键盘设置默认语言

[英]How to set a default language for the virtual keyboard of an EditText

I'm wanted to know if there any way i could set the default language of the virtual keyboard of an EditText in Android ? 我想知道是否有任何方法可以在Android EditText的虚拟键盘的默认语言?

Every time i get focus on my EditText it opens the virtual keyboard with my native language instead of English. 每当我专注于我的EditText它就会用我的母语而不是英语打开虚拟键盘。

I guess it something with the device settings, but if there is a way to program that in Android development that would be terrific. 我想这与设备设置有关,但如果有一种方法可以在Android开发中编程,那将是非常棒的。

LocaleHelper ” is the solution all you need. LocaleHelper “是您所需要的解决方案。 You just have to initialize locale on your application's main class. 您只需在应用程序的主类上初始化语言环境。 After that all your language changes will persist. 之后,您的所有语言更改都将持续存在。

In application class make following changes: 在应用程序类中进行以下更改:

    public class MainApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        LocaleHelper.onCreate(this, "en");
    }
}

LocalHelper.java will be: LocalHelper.java将是:

public class LocaleHelper {

private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

public static void onCreate(Context context) {
    String lang = getPersistedData(context, Locale.getDefault().getLanguage());
    setLocale(context, lang);
}

public static void onCreate(Context context, String defaultLanguage) {
    String lang = getPersistedData(context, defaultLanguage);
    setLocale(context, lang);
}

public static String getLanguage(Context context) {
    return getPersistedData(context, Locale.getDefault().getLanguage());
}

public static void setLocale(Context context, String language) {
    persist(context, language);
    updateResources(context, language);
}

private static String getPersistedData(Context context, String defaultLanguage) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}

private static void persist(Context context, String language) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();

    editor.putString(SELECTED_LANGUAGE, language);
    editor.apply();
}

private static void updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;

    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}

} }

For detail description you refer this link. 有关详细说明,请参阅链接。

Hope it will help you. 希望它会对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM