使用javascript for phonegap复制到剪贴板 [英] Copy to Clipboard with javascript for phonegap

查看:145
本文介绍了使用javascript for phonegap复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript / mobile-jquery界面为phonegap平台开发一个应用程序。现在我已经看到这么多的例子在网络上试图复制到剪贴板和unfortunatelly没有一个为我工作。我对浏览器的功能不感兴趣,只要它工作一旦它转换为电话差距我很高兴。

i am developing an app using javascript/mobile-jquery interface for the phonegap platform. Now I have seen so many examples on the web trying to copy to clipboard and unfortunatelly none of them worked for me. I am not interested in this being function-able in the browser, as long as it works once it is converted by phone gap I am happy.

我试过使用zeroclipboard ,它没有锻炼,我已经尝试使用剪贴板管理器无法得到它的工作。我试过很多其他的例子,我发现这里stackoverflow包括google搜索,他们仍然没有工作,这里是一个例子我试过的事情:

I have tried using zeroclipboard, it did not workout, I have tried using clipboard manager could not get it to work. I have tried many other examples that I found here on stackoverflow including google search and they still did not work, here is an example of things i've tried:

window.plugins.clipboardManager.copy(
                "the text to copy",
                function(r){alert("copy is successful")},
                function(e){alert(e)}
            );

我已包含js文件:

    <script src="src/clipboardmanager.js"></script>

我也有这样的文件夹结构中的java文件: src \com\saatcioglu\phonegap\clipboardmanager\ClipboardManagerPlugin.java

and I also have the java file in the folder structure as this: src\com\saatcioglu\phonegap\clipboardmanager\ClipboardManagerPlugin.java

根据我所阅读的内容,我需要包含xml文件,这是为了工作,但对于我的生活我找不到那个XML文件在任何地方。

From what I've read I need to include an xml file for this to work, but for the life of me I could not find that XML file anywhere.

任何帮助是最感激。

注意:我的应用程式不需要任何权限,例如相机,GPS等等。

Note: My app will require no permissions such as camera, gps, etc...

我尝试的另一个例子是:

Another example I tried was:

function select_all(obj) {
    var text_val=eval(obj);
    text_val.focus();
    text_val.select();
    if (!document.all) return; // IE only
    r = text_val.createTextRange();
    r.execCommand('copy');
}

这在IE中有效,但不在Phonegap。

This worked in IE but not in Phonegap.

编辑:

这里是我使用的html / javascript:

Here is the html/javascript I'm using:

<html>
    <head>
        <title>Test</title>
            <link rel="stylesheet" href="jquery/jquery.mobile-1.3.1.min.css" />
        <script src="jquery/jquery-1.9.1.min.js"></script>
        <script src="jquery/jquery.mobile-1.3.1.min.js"></script>
            <script src="clipboardmanager.js"></script>
        <script>
                var cbm = new window.plugins.clipboardManager;
                function main(textMessage)
                {
            //Some Code before this (calculations)
                    cbm.copy(
                        "Success!!!",
                        function(r){alert("copy is successful")},
                        function(e){alert(e)}
                    );
                }
            </script>
        </head>
        <body>
        <div data-role="page" id="main" name="main">
            <div data-role="header">
                <h1>Test</h1>
                </div><!-- /header -->

            <div data-role="content">
                <form action="javascript:main(encryptedMessage.value);">
                    Message to be Copied:
                    <textarea id="encryptedMessage" name="encryptedName" rows="6" style="width:99%;"></textarea>
                    <input type="submit" value="Encrypt" />
                </form>
                </div>
        </div>
    </body>
</html>

在我的根文件夹中,我有:

In my root folder I have:


  1. 一个名为jquery的文件夹,其中有jquery脚本。

  2. 一个名为res的文件夹,其名称为xml,文件名为
    .xml

  3. 一个名为src的文件夹,它有一个名为com的文件夹,其中有一个名为saatcioglu的
    文件夹,其名为phonegap,
    有一个文件夹称为剪贴板管理器,它有一个名为
    ClipboardManagerPlugin.java的文件。

  4. test.html

  5. clipboardmanager.js

  1. a folder called jquery which has jquery scripts in there.
  2. a folder called res which has a folder called xml which has a file called plugin.xml
  3. a folder called src which has a folder called com, which has a folder called saatcioglu, which has a folder called phonegap, which has a folder called clipboardmanager, which has a file called ClipboardManagerPlugin.java.
  4. test.html
  5. clipboardmanager.js

plugin.xml的内容

Contents of plugin.xml

<?xml version="1.0" encoding="utf-8"?>
<plugins>
    <gap:plugin name="clipboardmanager" value="com.saatcioglu.phonegap.clipboardmanager.ClipboardManagerPlugin.ClipboardManagerPlugin" />
</plugins>

我做错了什么?

推荐答案

首先:IE的选项不会在Android上工作PhoneGap使用Webkit(认为:Safari和/或Chrome)。

First up: that IE option will not work on Android as PhoneGap uses Webkit (think: Safari and/or Chrome).

无论如何...

您要查找的文件(在项目目录的/ res / xml /子目录)

That file you're looking for (in the "/res/xml/" subdirectory of your project's directory) is called

config.xml

在这里,你必须告诉phonegap在编译时加载插件,像这样...

In there, you have to tell phonegap to load the plugin at compile time like this...

<gap:plugin name="whatever" value="com.example.whatever" />

如果你不这样做,phonegap将不会在编译时包括插件,你的插件不会工作(因为它不存在于编译的 apk )。

If you don't do that, phonegap will simply not include the plugin at compile time, resulting in the fact that your plugin won't work (since it doesn't exist in the compiled apk).

我没有使用ClipboardManagerPlugin,但根据文档它应该有点像这样:

I haven't used the ClipboardManagerPlugin yet, but according to the docs it should go somewhat like this:

<gap:plugin name="clipboardmanager" value="com.saatcioglu.phonegap.clipboardmanager.ClipboardManagerPlugin.ClipboardManagerPlugin" />

请注意,您应该检查您使用的PhoneGap版本,如果插件与它兼容。为了防止你不知道它:并不是所有的插件已经更新,以与PhoneGap 3.x工作。引用自述在Github(https://build.phonegap.com/docs/plugins-using)除非明确,大​​多数这些插件将与Cordova / PhoneGap 3.xx开箱即用,他们需要更新,才能使用通过插件添加界面。

Please note that you should check the PhoneGap version you're using and if the plugin is compatible with it. Just in case you're not aware of it: not all plugins have been updated to work with PhoneGap 3.x yet. To quote the readme at Github (https://build.phonegap.com/docs/plugins-using): "Unless explicitly stated, most of these plugins will not work with Cordova/PhoneGap 3.x.x out of the box. They will need updating before they can be used via the plugin add interface."

这篇关于使用javascript for phonegap复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆