如何使用 JavaScript 隐藏 Android 键盘?

IT技术 javascript android keyboard
2021-01-29 17:15:32

我想在 JavaScript 中隐藏 Android 虚拟键盘。有人建议做

$('#input').focus(function() {
  this.blur();
});

但如果键盘已经可见,这将不起作用。这是可以做到的吗?

6个回答

我找到了一个更简单的解决方案,既不需要添加元素也不需要特殊类。在那里找到它:http : //www.sencha.com/forum/archive/index.php/t-141560.html

并将代码转换为 jquery :

function hideKeyboard(element) {
    element.attr('readonly', 'readonly'); // Force keyboard to hide on input field.
    element.attr('disabled', 'true'); // Force keyboard to hide on textarea field.
    setTimeout(function() {
        element.blur();  //actually close the keyboard
        // Remove readonly attribute after keyboard is hidden.
        element.removeAttr('readonly');
        element.removeAttr('disabled');
    }, 100);
}

您可以通过将打开键盘的输入传递给该函数来调用该函数,或者仅传递 $('input') 也应该起作用。

在 Pixel XL 上效果很好!
2021-03-12 17:15:32
根据此示例,建议围绕修改为“禁用”添加条件: if (element.is('textarea')) { element.attr('disabled', 'true'); } 否则 hideKeyboard 助手将导致文本输入闪烁。flash 的结果将根据移动浏览器和应用于禁用 textarea 的样式而有所不同。
2021-03-19 17:15:32
在运行 2.3.5 和 iPad 2 的 HTC EVO 上非常适合我
2021-03-29 17:15:32
更改attrsetAttributeremoveAttrremoveAttribute获取纯 JavaScript 解决方案。
2021-04-01 17:15:32
在带有 Android 2.3.6 的 Atrix 2 上,这个解决方案只对我有用过一次 - 现在不能再工作了。很奇怪。
2021-04-02 17:15:32

您需要做的是创建一个新的输入字段,将其附加到正文中,将其聚焦并使用display:none. 不幸的是,您需要将这些包含在一些 setTimeouts 中才能完成这项工作。

var field = document.createElement('input');
field.setAttribute('type', 'text');
document.body.appendChild(field);

setTimeout(function() {
    field.focus();
    setTimeout(function() {
        field.setAttribute('style', 'display:none;');
    }, 50);
}, 50);
@HaggleLad 哪个版本的 Android?Android 太牛逼了,所以很难跟上这些东西。
2021-03-16 17:15:32
@rdougan 我已经在 2.3.6 上的 Nexus S 和 2.2.1 上的 HTC Wildfire S 上尝试过,但不幸的是,这两者都不起作用。
2021-03-19 17:15:32
据我所知,这不适用于现代 Android 版本(4.0.3),并且它具有影响滚动的不幸后果(这可能可以通过将输入附加到正文以外的内容来帮助,但是由于它不起作用,没有多大意义)。
2021-03-22 17:15:32
你摇滚,rdougan,在互联网上跟着我并解决我所有的问题。我期待您的 Ext.hideKeyboard,我认为它使用了这种技术。:)
2021-03-26 17:15:32
在 Lollipop devices 上为我工作,但我还添加了 document.body.removeChild(field); 此外,对我来说不需要超时,同步运行所有代码。
2021-03-30 17:15:32

您现在可以在最新的浏览器上使用 inputmode="none"。看:

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode

最佳答案。这就是我要找的。
2021-03-16 17:15:32
这将是正确的答案。我们可以inputmode="none"在 HTML 元素上设置,在 (ngAfterViewInit) 视图启动之后,我们可以将 inputmode 设置为我们想要的任何内容(主要是inputmode="text")。它非常适合我!
2021-03-16 17:15:32
嗨@ingd,我也面临同样的问题,所以你有没有找到一些可以与扫描仪阅读器配合使用的东西来粘贴输入元素
2021-03-28 17:15:32
在 javascript 中,这个片段就像一个魅力: document.getElementById(objNextID).inputMode = "none";
2021-03-31 17:15:32
Inputmode="none" 可以很好地隐藏软键盘,但是当我使用 pda 的内置扫描仪阅读器时,我无法使用粘贴事件。
2021-04-08 17:15:32

这是适用于 Android 2.3.x 和 4.x 的防弹方法

您可以使用此链接测试此代码:http : //jsbin.com/pebomuda/14

function hideKeyboard() {
  //this set timeout needed for case when hideKeyborad
  //is called inside of 'onfocus' event handler
  setTimeout(function() {

    //creating temp field
    var field = document.createElement('input');
    field.setAttribute('type', 'text');
    //hiding temp field from peoples eyes
    //-webkit-user-modify is nessesary for Android 4.x
    field.setAttribute('style', 'position:absolute; top: 0px; opacity: 0; -webkit-user-modify: read-write-plaintext-only; left:0px;');
    document.body.appendChild(field);

    //adding onfocus event handler for out temp field
    field.onfocus = function(){
      //this timeout of 200ms is nessasary for Android 2.3.x
      setTimeout(function() {

        field.setAttribute('style', 'display:none;');
        setTimeout(function() {
          document.body.removeChild(field);
          document.body.focus();
        }, 14);

      }, 200);
    };
    //focusing it
    field.focus();

  }, 50);
}
凉爽的!这适用于带有 Lollipop 的 Galaxy Tab 4。同样适用于 iPhone 6 iOS 9.1
2021-03-19 17:15:32
它在 Android 6.0 上运行良好,但在 iOS 9 上它会导致视口放大。
2021-03-24 17:15:32
如果您设置 <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0"> 那么它会阻止在 iOS 上缩放...请参阅stackoverflow.com/questions/ 5002854/…
2021-03-25 17:15:32
使用默认浏览器在 android 6 设备上工作。
2021-03-31 17:15:32
使用位置:固定以避免滚动。
2021-04-09 17:15:32

对于使用带有cordova 的vuejs 或jquery 的任何人,请使用 document.activeElement.blur() ;

hideKeyboard() {
    document.activeElement.blur();
}

..从我的文本框中,我只是调用该函数:

对于VueJSv-on:keyup.enter="hideKeyboard" 按回车按钮关闭安卓键盘。

对于 jQuery:

$('element').keypress(function(e) {
  if(e.keyCode===13) document.activeElement.blur();
}
这就是我一直在寻找的。使用它在我的 React PWA 中隐藏 android(以及希望 ios)上的虚拟键盘。谢谢!
2021-03-15 17:15:32