gpt4 book ai didi

Android无法获取更改软键盘的 token

转载 作者:搜寻专家 更新时间:2023-11-01 08:39:43 26 4
gpt4 key购买 nike

我有一个自定义键盘,其中有一个按钮可以更改 IME....

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

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

但我只是得到 can't resolve 'getToken()'...我正在从扩展 InputMethodService 的类中执行此操作...什么是那么从键盘内部获取 token 的正确方法是什么?

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

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

最佳答案

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

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;
}

关于Android无法获取更改软键盘的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33911806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com