Show android keyboard from javascript

允我心安 提交于 2019-12-07 18:16:57

问题


I want to be able to show keyboard on mobile browser (e.g. Android) after navigating to the page. I have already seen some workarounds:

Show virtual keyboard on mobile phones in javascript

Can I trigger Android soft keyboard to open via javascript ( without phonegap )?

Showing Android's soft keyboard when a field is .focus()'d using javascript

But all of these examples work only if user tap somewhere. It allows to trigger focus() from within the click(). Does someone know if there any way to do the same thing but without user clicking?

I've tried to trigger click programmatically but it also not working. http://jsfiddle.net/alex_myronov/e5JcP/10/


回答1:


I managed to successfully open the virtual keyboard by just calling focus() method of the input.

It works in my app both on android 2.3 and 4.1.

I think the trick is that you have to wait untill the page is completly rendered before you call focus().

My app is using jquery mobile and I call focus() when the pageshow event is triggered:

$("#myPage").on("pageshow", function( event ) {
        $("#myPage").find('input:first').focus();
} );

Or maybe it's working because I'm in a phonegap app?




回答2:


My current answer to this is no, can't be done. The script that calls focus() click() on an input needs to be running with user context, ie. triggered by a user interaction. After fairly dedicated fiddling, I've found no way round this.



来源:https://stackoverflow.com/questions/21054795/show-android-keyboard-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!