How to detect keyboard language in textview?

enter image description here binding.etShortText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { String language = getKeyboardLanguage(); Log.d("language is", language); binding.txtDetectLang.setText(language); } } }); }

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


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.

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


I think you want to get language of the software keyboard's language. You can use InputMethodManager to get information about input types in Android.

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;
}
链接地址: http://www.djcxy.com/p/16676.html

上一篇: 在Android中完成按键时隐藏软键盘?

下一篇: 如何检测textview中的键盘语言?