How to determine the current IME in Android?

你说的曾经没有我的故事 提交于 2019-11-27 14:43:32

问题


I have an application where I would like to warn the user if they are not using the default Android softkeyboard. (i.e. they are using Swype or some thing else).

How can I check which input method they currently have selected?


回答1:


You can get a default IME, use:

Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);



回答2:


InputMethodManager has getEnabledInputMethodList(). You get an InputMethodManager from getSystemService() in your Activity.




回答3:


Here's a bit of code I used to determine if GoogleKeyboard, Samsung Keyboard, or Swype Keyboard is used. The value returned by reflection for mCurId indicates the IME ID.

Test with the different keyboards/input methods you are looking for to find the relevant one

public boolean usingSamsungKeyboard(Context context){
    return usingKeyboard(context, "com.sec.android.inputmethod/.SamsungKeypad");
}

public boolean usingSwypeKeyboard(Context context){
    return usingKeyboard(context, "com.nuance.swype.input/.IME");
}

public boolean usingGoogleKeyboard(Context context){
    return usingKeyboard(context, "com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME");
}   

public boolean usingKeyboard(Context context, String keyboardId)
    {
        final InputMethodManager richImm =
          (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);

        boolean isKeyboard = false;

        final Field field;
        try
        {
            field = richImm.getClass().getDeclaredField("mCurId");
            field.setAccessible(true);
            Object value = field.get(richImm);
            isKeyboard = value.equals(keyboardId);

        }
        catch (IllegalAccessException e)
        {

        }
        catch (NoSuchFieldException e)
        {

        }
        return  isKeyboard;
    }


来源:https://stackoverflow.com/questions/2744729/how-to-determine-the-current-ime-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!