如何使用JavaScript隐藏Android键盘?

Yoh*_*uki 47 javascript keyboard android

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

$('#input').focus(function() {
  this.blur();
});
Run Code Online (Sandbox Code Playgroud)

但是,如果键盘已经可见,则此功能无效.这是可以做到的吗?

Qui*_*Fix 58

我找到了一个更简单的解决方案,既不需要添加元素也不需要特殊类.在那里发现: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);
}
Run Code Online (Sandbox Code Playgroud)

你通过传递键盘打开输入来调用函数,或者只是传递$('input')也应该有效.

  • 对于纯 JavaScript 解决方案,将 `attr` 更改为 `setAttribute` 并将 `removeAttr` 更改为 `removeAttribute`。 (3认同)
  • 根据这个例子,建议在修改为'disabled'时添加一个条件:`if(element.is('textarea')){element.attr('disabled','true'); 否则hideKeyboard助手将导致文本输入闪烁.闪存的结果将根据移动浏览器和应用于禁用textareas的样式而有所不同. (2认同)

rdo*_*gan 43

您需要做的是创建一个新的输入字段,将其附加到正文,聚焦它并使用它隐藏它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);
Run Code Online (Sandbox Code Playgroud)

  • @HaggleLad哪个版本的Android?Android是如此的错误,所以很难跟上这些事情. (7认同)
  • 你摇滚,rdougan,在互联网上跟着我并解决我所有的问题。我期待您的 Ext.hideKeyboard,我认为它使用了这种技术。:) (2认同)
  • @rdougan 我已经在 2.3.6 上的 Nexus S 和 2.2.1 上的 HTC Wildfire S 上尝试过,但不幸的是,这两者都不起作用。 (2认同)
  • 据我所知,这在现代Android版本(4.0.3)上不起作用,并且它有影响滚动的不幸结果(可能通过将输入附加到身体以外的其他东西可能有所帮助,但是它不起作用没有多大意义). (2认同)
  • 在 Lollipop devices 上为我工作,但我还添加了 document.body.removeChild(field); 此外,对我来说不需要超时,同步运行所有代码。 (2认同)

obe*_*iro 20

这是一个适用于Android 2.3.x和4.x的booletprouf方法

您可以使用以下链接测试此代码: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);
}
Run Code Online (Sandbox Code Playgroud)


小智 20

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

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

  • 最佳答案。这就是我一直在寻找的。 (3认同)
  • Inputmode =“none”可以很好地隐藏软键盘,但是当我使用pda的内置扫描仪阅读器时,我无法使用粘贴事件。 (2认同)

The*_*One 7

只需模糊有效的焦点输入字段即可:

$(document.activeElement).filter(':input:focus').blur();
Run Code Online (Sandbox Code Playgroud)

  • 如果您来自 2018 年,这个答案就很好用(至少在我的 android 设备上)。 (4认同)
  • 如果就这么简单...(至少不能在Android 2.3.6上使用) (3认同)

Ale*_*lex 5

如果找不到解决方案,可以随时从javascript调用Java代码。这里有教程和示例。在此处隐藏软键盘。

...
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
....

public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    public void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript

<script type="text/javascript">
function hideAndroidKeyboard() {
    Android.hideKeyboard();
}
</script>
Run Code Online (Sandbox Code Playgroud)

注意事项:

从Java到Native Java的Javascript无法在Simulator 2.3+版本上运行。http://code.google.com/p/android/issues/detail?id=12987

我不确定,但是在调用hideKeyboard时,您可能不在主线程上。

当然,如果您找不到简单的解决方案。

  • 我一直在寻找仅JavaScript的解决方案。使用本机代码来完成它并从JavaScript调用它对我而言并不是一件好事。 (2认同)

zev*_*ero 5

我从 QuickFix 的答案中制作了一个 jquery 插件

(function ($) {
  $.fn.hideKeyboard = function() {
    var inputs = this.filter("input").attr('readonly', 'readonly'); // Force keyboard to hide on input field.
    var textareas = this.filter("textarea").attr('disabled', 'true'); // Force keyboard to hide on textarea field.
    setTimeout(function() {
      inputs.blur().removeAttr('readonly');  //actually close the keyboard and remove attributes
      textareas.blur().removeAttr('disabled');
    }, 100);
    return this;
  };
}( jQuery ));
Run Code Online (Sandbox Code Playgroud)

用法示例:

$('#myInput').hideKeyboard(); 
$('#myForm input,#myForm textarea').hideKeyboard(); 
Run Code Online (Sandbox Code Playgroud)


Eri*_*num 5

对于将vuejs或jquery与cordova一起使用的任何人,请使用document.activeElement.blur();

hideKeyboard() {
    document.activeElement.blur();
}
Run Code Online (Sandbox Code Playgroud)

..然后从我的文本框中调用该函数:

对于VueJSv-on:keyup.enter="hideKeyboard" 按Enter键可关闭android键盘。

对于jQuery:

$('element').keypress(function(e) {
  if(e.keyCode===13) document.activeElement.blur();
}
Run Code Online (Sandbox Code Playgroud)