如何在textview中检测键盘语言?

时间:2018-03-23 12:03:31

标签: android

enter image description here 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();     }

2 个答案:

答案 0 :(得分:0)

我想你想要获得软件键盘语言的语言。您可以使用InputMethodManager获取有关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;
}

答案 1 :(得分:-1)

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.

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