CocosCreator获取和设置剪切版

Android 原生环境

在Android环境中,可以使用android.content.ClipboardManager来复制和粘贴数据

// 获取黏贴版内容
public static String getPasteBoardContent(final long delayTime) {
        result = "";
        long deadLine = delayTime + SystemClock.elapsedRealtime();
        AppActivity.appActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) AppActivity.appActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                if (clipboardManager == null) {

                } else if (!clipboardManager.hasPrimaryClip()) {

                } else if (!clipboardManager.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {

                } else {
                    result = clipboardManager.getPrimaryClip().getItemAt(0).getText().toString();
                }
            }
        });
        while (result.isEmpty()) {
            if (SystemClock.elapsedRealtime() > deadLine) {
                break;
            }
        }
        return result;
    }

// 设置黏贴版内容
public static void setPasteBoardContent(final String str) {
        AppActivity.appActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) AppActivity.appActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                if (clipboardManager != null) {
                    ClipData clipdata = ClipData.newPlainText(AppActivity.appActivity.getPackageName(), str);
                    clipboardManager.setPrimaryClip(clipdata);
                }
            }
        });
    }

iOS 原生环境

在iOS环境中,可以使用UIPasteboard来复制和粘贴数据

// 获取黏贴版内容
+(NSString*) getPasteBoardContent{
    UIPasteboard *pasteboard=[UIPasteboard generalPasteboard];
    NSString* result=@"";

    double version=[[[UIDevice currentDevice]systemVersion]doubleValue];
    if(version>= 10.0)
    {
        if([pasteboard hasStrings])
        {
            result=pasteboard.string;
        }
    }
    else
    {
        result=pasteboard.string;
    }
    return result;
}

// 设置黏贴版内容
+(void)setPasteBoardContent:(NSString*)data{
    UIPasteboard *pasteboard=[UIPasteboard generalPasteboard];
    [pasteboard setString:data];
}

H5 网页环境

在网上中复制粘贴是最麻烦的,要考虑到不同系统不同浏览器的情况,复制(设置剪切版)稍微还好一点,但是要获取剪切版的内容就太麻烦了,暂时还未找到兼容性很好的方法。

//设置剪切板
    public SetPasteBoardContent(content) {
        if (cc.sys.os == cc.sys.OS_IOS) {
            let inputObj = document.createElement("input");
            inputObj.value = content;
            document.body.appendChild(inputObj);
            inputObj.select();
            inputObj.setSelectionRange(0, inputObj.value.length);
            document.execCommand('Copy');
            document.body.removeChild(inputObj);
        } else {
            let domObj = document.createElement("span");
            domObj.innerHTML = content;
            document.body.appendChild(domObj);
            let selection = window.getSelection();
            let range = document.createRange();
            range.selectNodeContents(domObj);
            selection.removeAllRanges();
            selection.addRange(range);
            document.execCommand('Copy');
            document.body.removeChild(domObj);
        }
    }
0%