Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检测是否已选择输入法_Android_Android Input Method - Fatal编程技术网

Android 检测是否已选择输入法

Android 检测是否已选择输入法,android,android-input-method,Android,Android Input Method,在我的应用程序中,我需要让用户选择输入法。一旦它被选中,我应该执行一些任务。如何检测用户实际选择的输入方法 这是用于显示InputMethod列表的代码 InputMethodManager imeManager = (InputMethodManager) mw.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); if (imeManager != null) { imeManager.show

在我的应用程序中,我需要让用户选择输入法。一旦它被选中,我应该执行一些任务。如何检测用户实际选择的
输入方法

这是用于显示
InputMethod
列表的代码

InputMethodManager imeManager = (InputMethodManager) mw.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imeManager != null) {
    imeManager.showInputMethodPicker();
} else {
    Toast.makeText(mw.getApplicationContext(), "IME ERROR",
    Toast.LENGTH_LONG).show();
}

不幸的是,您无法捕获用户在
InputMethodPicker
中选择的输入法

但是,您可以在用户拾取后使用
BroadcastReceiver

当IME更改时,将广播
Intent.ACTION\u INPUT\u METHOD\u CHANGED

public class InputMethodChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_INPUT_METHOD_CHANGED)) {
            .....
            /* You can check the package name of current IME here.*/
        }
    }
}
然后,注册它

IntentFilter filter = new IntentFilter(Intent.ACTION_INPUT_METHOD_CHANGED);
registerReceiver(mReceiver, filter);

即使您接受了答案,也有一种方法可以检查用户是否选择您的键盘作为默认键盘:

public static boolean isThisKeyboardSetAsDefaultIME(Context context) {
    final String defaultIME = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
    return isThisKeyboardSetAsDefaultIME(defaultIME, context.getPackageName());
}

public static boolean isThisKeyboardSetAsDefaultIME(String defaultIME, String myPackageName) {
    if (TextUtils.isEmpty(defaultIME))
        return false;

    ComponentName defaultInputMethod = ComponentName.unflattenFromString(defaultIME);
    if (defaultInputMethod.getPackageName().equals(myPackageName)) {
        return true;
    } else {
        return false;
    }
}
您可以将此代码与此代码组合,一旦用户从InputMethodPicker中选择键盘,请检查您的键盘是否为默认键盘(表示用户选择了它)


如果您对实现键盘有更多问题,请检查此项。干杯。

我这样做是为了检测我的ime是否被选中或启用

为了检查,我选择了

 public static boolean isThisKeyboardSelected(Context context) {
        final String defaultIME = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
        if (TextUtils.isEmpty(defaultIME))
            return false;
        ComponentName defaultInputMethod = ComponentName.unflattenFromString(defaultIME);
//        return defaultInputMethod.getPackageName().equals(myPackageName);
        return defaultIME.contains("your package Name here");

    }



     public static boolean isThisKeyboardSetAsDefaultIME(Context context) {
        final String defaultIME = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_INPUT_METHODS);
        if (TextUtils.isEmpty(defaultIME))
            return false;
        ComponentName defaultInputMethod = ComponentName.unflattenFromString(defaultIME);
//        return defaultInputMethod.getPackageName().equals(myPackageName);
        return defaultIME.contains("your package name);

    }

“在我的应用程序中,我需要让用户选择一种输入法”--为什么?@commonware,因为它处理特定输入设备的行为。谢谢,这就足够满足我的需要了!