我有一个自定义键盘,我有一个更改IME的按钮....
从Android示例代码中,我发现了以下内容:
InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imeManager.switchToNextInputMethod(getToken(), false /* onlyCurrentIme */);
但我只是得到can't resolve 'getToken()'
...我是从一个扩展InputMethodService
的类中做到的...那么从键盘内部获取令牌的正确方法是什么?< / p>
如果不是上述内容,那么这个也可以起作用:
imeManager.showInputMethodPicker();
imeManager.hideSoftInputFromWindow(/*token?*/, 0);
我的课程:
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);
答案 0 :(得分:2)
所以我的诀窍是使用以下内容:
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;
}
答案 1 :(得分:0)
你可以试试这样的事情
public void fun(View view) {
InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imeManager.switchToNextInputMethod(view.getWindowToken(), false /* onlyCurrentIme */);
}
答案 2 :(得分:0)
这对我有用:
IBinder token = getWindow().getWindow().getAttributes().token;
可以在InputMethodService
内的任何地方使用。