简体   繁体   English

trigger.io - 关闭软键盘?

[英]trigger.io - close soft keyboard?

Is there a way to programatically close the Android on screen keyboard with trigger.io? 有没有办法用trigger.io以编程方式关闭Android屏幕键盘?

I have a search field with an auto-complete search on. 我有一个自动完成搜索的搜索字段。 When the user stops typing I show the search results, but the onscreen keyboard stays visible - obscuring a number of the results. 当用户停止键入时,我会显示搜索结果,但屏幕键盘仍然可见-遮盖了许多结果。

There are actually two ways of doing this using Trigger.io : 使用Trigger.io实际上有两种方法:

Native plugin 本机插件

You can write a native plugin that hides the soft keyboard. 您可以编写隐藏软键盘的本机插件 The relevant code should look something like this (based on Close/hide the Android Soft Keyboard ): 相关代码应如下所示(基于关闭/隐藏Android软键盘 ):

InputMethodManager inputMethodManager = (InputMethodManager) ForgeApp.getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(ForgeApp.getActivity().getCurrentFocus().getWindowToken(), 0);

Javascript 使用Javascript

The proper way to hide the soft keyboard using Javascript would be to blur the element that is currently focused. 使用Javascript隐藏软键盘的正确方法是模糊当前关注的元素。 In modern browsers, you just need to call: 在现代浏览器中,您只需要调用:

document.activeElement.blur()

However, document.activeElement is not always available and sometimes seems to be incorrect. 但是, document.activeElement并不总是可用,有时似乎不正确。 I use something like this: 我使用这样的东西:

if (document.activeElement && 
    document.activeElement.blur && 
    document.activeElement !== document.body) {
  document.activeElement.blur();
}
else {
  jQuery(':focus').blur();
}

Even this will not work for some Android 2.x devices. 即使这样也不适用于某些 Android 2.x设备。 Check out How can I hide the Android keyboard using JavaScript? 查看如何使用JavaScript隐藏Android键盘? for more workarounds. 有关更多解决方法。

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

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