如何知道是否启用了“InputMethod”?

How to know if an `InputMethod` is enabled?

我正在从头开始编写自定义 InputMethod,并希望在我的 Activity 中向用户显示一个按钮,以便在我的 InputMethod 被禁用时启用它...

我需要以编程方式查明我的 InputMethod 是否已在设备中启用。

我该怎么做?

您可以使用 InputMethodManager 获取已启用的列表 InputMethodInfo 并遍历它以查明您的 InputMethod 是否已启用。

public boolean isMyInputMethodEnabled() {
    boolean isEnabled = false;

    InputMethodManager inputMethodManager
            = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    List<InputMethodInfo> inputMethodList = inputMethodManager
            .getEnabledInputMethodList();

    for (InputMethodInfo inputMethodInfo : inputMethodList) {
        if (inputMethodInfo.getPackageName().equals(getPackageName())) {
            isEnabled = true;
            break;
        }
    }

    return isEnabled;
}