Cocos2dx 复制文本到剪切板(Android 和 ios)
至于c++调用java lua调用java 等方法这里不介绍了。下面本人真机亲测。如果有问题请留言交流。
1.Android
实现方式主要看 ClipboardManager
ClipboardManager cmb = (ClipboardManager)
getSystemService(CLIPBOARD_SERVICE);
cbm.setText(et.getText().toString());
1、参考文档:Android 复制文本内容到系统剪贴板的最简单实践
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(要复制的文本内容);
源码分享:
Cocos2dxHelper.java 中添加吧
import android.content.ClipboardManager; //导入需要的库
static public int copyToClipboard(final String text)
{
try
{
//Log.d("cocos2dx","copyToClipboard " + text);
Runnable runnable = new Runnable() {
public void run() {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) Cocos2dxActivity.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", text);
clipboard.setPrimaryClip(clip);
}
};
//getSystemService运行所在线程必须执行过Looper.prepare()
//否则会出现Can't create handler inside thread that has not called Looper.prepare()
((Cocos2dxActivity)sActivity).runOnUiThread(runnable);
}catch(Exception e){
// Log.d("cocos2dx","copyToClipboard error");
e.printStackTrace();
return -1;
}
return 0;
}
2、ios-复制字符串到剪贴板
/*复制到剪切板*/
int Application::copyToClipboard(const std::string &text){
//把char*转换成OC的NSString
NSString *nsMessage= [NSString stringWithUTF8String:text.c_str()];
//获得ios的剪切板
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
//改变剪切板的内容
pasteboard.string = nsMessage;
return 0;
}
参考文档: