Get keyboard language or detect user input languag

2019-02-07 05:11发布

Problem Description

I'm trying to detect current selected language of the keyboard. For that purpose I use following code:

Code

/* Return the handle to a system-level service by name. The class of the returned
 * object varies by the requested name. */
 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
/* Returns the current input method subtype. This subtype is one of the subtypes in the
 * current input method. This method returns null when the current input method doesn't
 * have any input method subtype. */
 InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
/* The locale of the subtype. This method returns the "locale" string parameter passed
 * to the constructor. */
 String locale = ims.getLocale();

but application throes NoSuchMethodError exception on getCurrentInputMethodSubtype function.

Question

Is there any way to get Keyboard selected language? If no how I can detect the language in which user typing in the application?

5条回答
等我变得足够好
2楼-- · 2019-02-07 05:33

If you want to get the selected language of your device. This might help u

Locale.getDefault().getDisplayLanguage();

You might also do:

Locale.getDefault().toString() which gives a string that fully identifies the locale, e.g. "en_US".

查看更多
迷人小祖宗
3楼-- · 2019-02-07 05:34

No. There's a phone locale, but the keyboard may ignore it by design (many keyboards allow you to switch languages just within the keyboard for bilingual users). There's no API for the keyboard to report it has done so to the OS.

查看更多
forever°为你锁心
4楼-- · 2019-02-07 05:36

You can get keyboard language by first detecting the locale of device keyboard and then getting the Locale object from it. For example,

    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();

Here, currentLanguage will give your keyboard language. Hope this helps.

查看更多
祖国的老花朵
5楼-- · 2019-02-07 05:48

1st Method

To get the selected language of your device. This might help u

Locale.getDefault().getLanguage()        ---> en     
Locale.getDefault().getISO3Language()    ---> eng
Locale.getDefault().getCountry()         ---> US
Locale.getDefault().getISO3Country()     ---> USA
Locale.getDefault().toString()           ---> en_US
Locale.getDefault().getDisplayLanguage() ---> English
Locale.getDefault().getDisplayCountry()  ---> United States
Locale.getDefault().getDisplayName()     ---> English (United States)

2nd Method

You can extract the language from the current locale. You can extract the locale via the standard Java API, or by using the Android Context. For instance, the two lines below are equivalent:

String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();
查看更多
家丑人穷心不美
6楼-- · 2019-02-07 05:50

Here's what I did to get the available input languages:

private void printInputLanguages() {
   InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
   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();
             Log.i(TAG, "Available input method locale: " + currentLocale);
          }
       }
   }
}
查看更多
登录 后发表回答