[英]How to close/hide custom keyboard Android
我尝试在 gridview 中单击项目后关闭我的自定义键盘。我正在尝试在 BaseAdapter 类中执行此操作。 上下文来自 InputMethodService。
到目前为止,我已经尝试过:
FrameLayout scroll = (FrameLayout)inflater.inflate(R.layout.keyboard, null);
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(scroll.getWindowToken(), 0);
——
imm.toggleSoftInput(0,InputMethodManager.HIDE_IMPLICIT_ONLY);
——
scroll.setVisibility(View.INVISIBLE);
如果您有自己的自定义键盘并且扩展了InputMethodService
,那么您只需调用
requestHideSelf(0)
从您的服务中强制关闭键盘或
requestHideSelf(InputMethodManager.HIDE_IMPLICIT_ONLY);
仅当用户没有明确要求显示键盘时才关闭键盘。
文档
我只是在这里从我的应用程序复制和粘贴,它对我们来说很好用:
public static void hideKeyboard(View v) {
try {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
} catch (Exception e) {
// we all saw shit happening on this code before
}
}
您可以将此方法放在公共类中,并在需要的地方调用它。
public static void hideKeyboard(Context ctx) {
InputMethodManager inputManager = (InputMethodManager) ctx
.getSystemService(Context.INPUT_METHOD_SERVICE);
// check if no view has focus:
View v = ((Activity) ctx).getCurrentFocus();
if (v == null)
return;
inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
在这里参考我的回答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.