简体   繁体   English

如何使用PhoneGap的软键盘(org.apache.cordova.plugin.softkeyboard)?

[英]How do I use PhoneGap's soft keyboard (org.apache.cordova.plugin.softkeyboard)?

I'm writing my first PhoneGap app, and I need to make the keyboard appear and disappear programmatically. 我正在编写我的第一个PhoneGap应用程序,并且需要以编程方式使键盘出现和消失。 Unfortunately, it's not working, but I wonder if I'm doing it correctly. 不幸的是,它无法正常工作,但是我想知道自己是否正确执行了操作。 Here's the state of things: 这是事情的状态:

I used $ cordova plugin search keyboard to find this plugin at the command line, and then I installed it like this: 我使用$ cordova plugin search keyboard在命令行上找到了这个插件,然后像这样安装了它:

$ cordova plugin add org.apache.cordova.plugin.softkeyboard Fetching plugin "org.apache.cordova.plugin.softkeyboard" via plugin registry Installing "org.apache.cordova.plugin.softkeyboard" for android

Based on another SO answer , I then added this line to my config.xml: 根据另一个SO答案 ,然后将以下行添加到我的config.xml中:

<plugin name="SoftKeyBoard" value="org.apache.cordova.plugin.SoftKeyBoard" />

I put softkeyboard.js in myapp/www/js/, and I referenced it in the html like so: 我将softkeyboard.js放在myapp / www / js /中,并在html中引用了它,如下所示:

<script type="text/javascript" charset="utf-8" src="js/softkeyboard.js"></script>

After installing, SoftKeyboard.java appeared in myapp/plugins/org.apache.cordova.plugin.softkeyboard . 安装后,SoftKeyboard.java出现在myapp / plugins / org.apache.cordova.plugin.softkeyboard中。 But just for good measure I also put a copy in myapp/src/org/apache/cordova/plugin/ . 但是出于很好的考虑,我也将副本放在myapp / src / org / apache / cordova / plugin /中。

Finally, I added these to my html: 最后,我将它们添加到了html中:

<button id="keyboard">Toggle Keyboard</button>

and

<script> var softkeyboard = window.cordova.plugins.SoftKeyBoard; $('#keyboard').toggle(softkeyboard.show, softkeyboard.hide); </script>

As you might have guessed, nothing happened when I rebuilt and clicked the button. 您可能已经猜到了,当我重建并单击按钮时,什么也没发生。 What should I be doing differently? 我应该怎么做?

Thanks! 谢谢!

PS - After seeing plugin (singular) and plugins (plural) used in so many SO answers, I renamed everything both ways and that didn't affect the outcome. PS-在看到这么多的SO答案中使用的插件(单数)和插件(复数)后,我将两种方式都重命名了,并且不影响结果。

After adding plugin with cordova add plugin command, it's not necessary to include plugin's js file. 使用cordova add plugin命令cordova add plugin ,无需包含插件的js文件。 You only need to include cordova.js (I think you have already done that, but better to clarify): 您只需要包含cordova.js(我想您已经做到了,但是最好澄清一下):

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>

After adding plugin, you should build or prepare project to get plugin in action. 添加插件后,您应该构建或准备项目以使插件生效。 Cordova cli tool will copy the javascript and native (android, ios, etc) codes into platforms/%platform_name% directory. Cordova cli工具会将javascript和本机(android,ios等)代码复制到platforms/%platform_name%目录中。 Plugins will not work in browser mostly, but never in /www directory (as there should be no cordova.js inside that dir). 插件大部分不会在浏览器中工作,但不会在/www目录中工作(因为该目录中应该没有cordova.js)。

I think the problem here is jQuery toggle function that you use. 我认为这里的问题是您使用的jQuery切换功能。 It is actually a function for toggling display state of the element. 它实际上是用于切换元素的显示状态的功能。 (see: http://api.jquery.com/toggle/ ) (请参阅: http : //api.jquery.com/toggle/

This will work when you emulate or run your android app. 当您模拟或运行您的android应用程序时,这将起作用。

<script>
   var isKeyboardShown = false;
   var softkeyboard = window.cordova.plugins.SoftKeyBoard;
   $('#keyboard').click(function() {
       (isKeyboardShown = !isKeyboardShown) ? softkeyboard.show() : softkeyboard.hide();
   });
</script>

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

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