我正在编写一个使用编辑文本的应用程序。我想知道用户在 oncreate 方法中输入的语言是什么,或者键盘输入类型何时更改。有没有办法设置一个监听器来获得这种变化?我找到的所有答案都是关于语言环境而不是关于输入语言的。我不想处理语言环境,语言环境对应用程序没有影响,输入语言有影响。我用过这个代码。 private void printInputLanguages() {
List<InputMethodInfo> ims = imm.getEnabledInputMethodList();
for (InputMethodInfo method : ims) {
List<InputMethodSubtype> submethods = imm
.getEnabledInputMethodSubtypeList(method, true);
for (InputMethodSubtype submethod : submethods) {
if (submethod.getMode().equals("keyboard")) {
String currentLocale =
submethod.getLocale();
tv.setText(currentLocale);
Log.i("sara", "Available input method locale: "
+ currentLocale);
}
}
}
还有这些代码,但没有一个不是完整的。
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String localeString = ims.getLocale();
Locale locale = new Locale(localeString);
String currentLanguage = locale.getDisplayLanguage();
我使用了这个链接,但还是无法解决我的问题
Get keyboard language or detect user input language in Android
请有人帮助我知道在 Activity 创建时获取用户输入法。 谢谢。
最佳答案
请检查一下:我认为您需要的所有东西都在里面,请注意它是完全可用的代码。
Android: Detect softkeyboard RTL Vs LTR (language direction)
这里是检测软键盘当前语言的短代码(屏幕键盘 - 输入法语言):localeDisplayName
是键盘的语言...
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype inputMethodSubtype = inputMethodManager.getCurrentInputMethodSubtype();
Locale mLocale = new Locale(inputMethodSubtype.getLocale());
String localeDisplayName = mLocale.getDisplayName(); //e.g. "English"
关于android - 如何获取用户输入法语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33441338/