简体   繁体   English

如何关闭/隐藏自定义键盘 Android

[英]How to close/hide custom keyboard Android

I've try to close my custom keyboard after click item in gridview.I'm trying to do it in BaseAdapter class.我尝试在 gridview 中单击项目后关闭我的自定义键盘。我正在尝试在 BaseAdapter 类中执行此操作。 context is come from InputMethodService.上下文来自 InputMethodService。

So far I've tried below:到目前为止,我已经尝试过:

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

If you have your own custom keyboard and you have extended InputMethodService , then you can just call如果您有自己的自定义键盘并且扩展了InputMethodService ,那么您只需调用

requestHideSelf(0)

from within your service to force close the keyboard or从您的服务中强制关闭键盘或

requestHideSelf(InputMethodManager.HIDE_IMPLICIT_ONLY);

to close the keyboard only if the user didn't explicitly request it to be shown.仅当用户没有明确要求显示键盘时才关闭键盘。

Documentation文档

I'm just copying and pasting from my app here, it works fine for us:我只是在这里从我的应用程序复制和粘贴,它对我们来说很好用:

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

You can put this method in a public class, and call it wherever needed.您可以将此方法放在公共类中,并在需要的地方调用它。

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

refer to my answer here在这里参考我的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM