关于android:如何在textview中检测键盘语言?

How to detect keyboard language in textview?

在此处输入图像描述binding.etShortText.setOnFocusChangeListener(new View.OnFocusChangeListener(){
@覆盖
public void onFocusChange(View v,boolean hasFocus){
if(hasFocus){
String language = getKeyboardLanguage();
Log.d("语言是",语言);
binding.txtDetectLang.setText(语言);
}
}
});
}

private String getKeyboardLanguage(){
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype inputMethodSubtype = inputMethodManager.getCurrentInputMethodSubtype();
return inputMethodSubtype.getLocale();
}


我想你想要获得软件键盘语言的语言。 您可以使用InputMethodManager获取有关Android中输入类型的信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private String getKeyboardLanguage(){
    String language ="";

    // This will give you to access input method manager
    InputMethodManager im = (InputMethodManager)Context.getSystemService(Context.INPUT_METHOD_SERVICE);

    // Input methods has several subtypes, this will return the active one
    InputMethodSubtype subtype = im.getCurrentInputMethodSubtype();

    if(subtype != null){ // subtype can be null
        // Returns BCP-47 Language Tag or returns empty string if not specified
        language = getLanguageTag();
    }
    return language;
}


1
2
3
4
5
6
7
8
9
10
11
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();

String locale = ims.getLocale();

//get Locale object first
Locale locale = new Locale(localeString);
//then get the display language from locale object.
String currentLanguage = locale.getDisplayLanguage();
//Also note that this method is deprecated in API 24. For API 24 or further use getLanguageTag() method.

您可以尝试使用此代码获取当前键盘语言区域代码。