我想知道是否有任何方法可以在Android
中设置EditText
虚拟键盘的默认语言?
每次我把注意力集中在EditText
上时,它都会用我的母语而不是英语打开虚拟键盘。
我想这和设备设置有关,但如果有一种方法可以在Android
开发中编程,那将是非常棒的。
LocaleHelper
"是您所需要的全部解决方案。您只需要在应用程序的主类上初始化区域设置。在那之后,你所有的语言变化都会持续下去。
在应用程序类中进行以下更改:
public class MainApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
LocaleHelper.onCreate(this, "en");
}
}
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());
}
}
有关详细说明,请参阅此链接。
希望它能帮助你。