Android 无法获取更改软键盘的令牌

Android can't get token to change soft keyboard

我有一个自定义键盘,其中有一个用于更改 IME 的按钮....

从 Android 示例代码中,我发现了以下内容:

InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imeManager.switchToNextInputMethod(getToken(), false /* onlyCurrentIme */);

但我只是得到 can't resolve 'getToken()'...我正在从扩展 InputMethodService 的 class 中执行此操作...从中获取令牌的正确方法是什么那么在键盘里面呢?

如果不是上面的,那么这个也可以:

imeManager.showInputMethodPicker();
imeManager.hideSoftInputFromWindow(/*token?*/, 0);

我的class:

public class CustomKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

    private KeyboardView kv;
    private Keyboard keyboard;

    public final static int CodeRowFourGlobe = 55041;

    @Override
    public View onCreateInputView() {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.custom_keyboard);
        kv.setPreviewEnabled(false);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    public void closeKeyboard(View view) {
        InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imeManager.switchToNextInputMethod(view.getWindowToken(), false /* onlyCurrentIme */);
    }

    @Override
    public void onPress(int primaryCode) {

    }

    @Override
    public void onRelease(int primaryCode) {

    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();

        if (primaryCode == Keyboard.KEYCODE_DELETE) {
            ic.deleteSurroundingText(1, 0);
        } else if (primaryCode == CodeRowFourGlobe) {
            InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imeManager != null) {
                Log.d(TAG, "Close keyboard");

            } else {
                Toast.makeText(this, R.string.no_keyboard_available, Toast.LENGTH_LONG).show();
            }
        } else {

        }
    }
}

我试过:

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(kv.getApplicationWindowToken(), 0);
imm.switchToNextInputMethod(kv.getApplicationWindowToken(), false);
imm.switchToNextInputMethod(kv.getWindowToken(), false /* onlyCurrentIme */);
imm.hideSoftInputFromWindow(kv.getWindowToken(), 0);

你可以试试这样的

public void fun(View view) {
       InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
       imeManager.switchToNextInputMethod(view.getWindowToken(), false /* onlyCurrentIme */);
}

所以我的诀窍是使用以下内容:

private IBinder getToken() {
    final Dialog dialog = getWindow();
    if (dialog == null) {
        return null;
    }
    final Window window = dialog.getWindow();
    if (window == null) {
        return null;
    }
    return window.getAttributes().token;
}

这对我有用:

IBinder token = getWindow().getWindow().getAttributes().token;

可以在 InputMethodService 内的任何地方使用。