cocos2dx 实现文字的一键复制功能(IOS、Android)

1、IOS篇(用OC和C++混编)

头文件声明:

public static void copy(std::string str);

实现如下:

void copy(std::string str) {

    //string类型转换成为char*

    char*p=(char*)str.data();

    //char*转换成OCNSString

    NSString *nsMessage= [[NSStringalloc] initWithCString:pencoding:NSUTF8StringEncoding];

    //获得iOS的剪切板

    UIPasteboard *pasteboard = [UIPasteboardgeneralPasteboard];

    //改变剪切板的内容

    pasteboard.string = nsMessage;

}


2、Android篇

public void copy(final String str)

{

Runnable runnable = new Runnable() {
public void run() {
ClipboardManager mClipboardManager = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
mClipboardManager.setText(str);
}
};
runOnUiThread(runnable);

}

你可能感兴趣的:(cocos2dx 实现文字的一键复制功能(IOS、Android))