在app / build.gradle中:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
compileSdkVersion 26
flavorDimensions "default"
defaultConfig {
applicationId "com.myapp"
minSdkVersion 17
targetSdkVersion 26
versionCode 78
versionName "3.36"
在软键盘上单击更改语言的按钮时,我想检测当前键盘的语言:
在片段中,我尝试获取当前的键盘语言,如下所示:
@Override
public void afterTextChanged(Editable editable) {
if (editable.length() == 1) {
System.out.println("Locale.getDefault().getLanguage() = " + RTLUtils.getCurrentInputLanguageLocale(getActivity()));
}
}
public static String getCurrentInputLanguageLocale(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String locale = ims.getLocale();
return locale;
}
但总是返回空的语言环境