cocos2dx剪切板
程序员文章站
2022-06-14 13:37:07
...
本文是cocos2dx lua为例子, cocos creator js也是一样的只是桥接代码不同罢了
android 平台剪切板
主要考虑ui线程和gl线程的问题
//设置剪切板内容
public static void setClipBoard(final String strName)
{
if(strName.trim().equals("")){
return;
}
s_instance.runOnUiThread(new Runnable()
{
@Override
public void run()
{
if(android.os.Build.VERSION.SDK_INT>11) //=android.os.Build.VERSION_CODES.HONEYCOMB
{
//Context ct = new Context();
ClipboardManager clipboardManager = (ClipboardManager) s_instance.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData cd = ClipData.newPlainText("label", strName);
clipboardManager.setPrimaryClip(cd);
Toast.makeText(s_instance, "内容已复制到剪贴板", Toast.LENGTH_SHORT).show();
}
}
});
}
public static void getClipBoardEx(final int luaFunctionId) {
s_instance.runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
tempStr = "";
ClipboardManager clipboardManager = (ClipboardManager)s_instance.getSystemService(Context.CLIPBOARD_SERVICE);
if(clipboardManager != null){
ClipData localClipData = clipboardManager.getPrimaryClip();
if(localClipData != null){
ClipData.Item item = localClipData .getItemAt(0);
if(item != null){
CharSequence seq = item.getText();
if(seq != null && seq.toString() != null ){
tempStr = seq.toString().trim();
}
}
}
}
s_instance.runOnGLThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Cocos2dxLuaJavaBridge.callLuaFunctionWithString(luaFunctionId, tempStr);
Cocos2dxLuaJavaBridge.releaseLuaFunction(luaFunctionId);
}
});
}
});
}
IOS平台
ios平台相对简单,需要做一些空字符串或者获取为空的判断
+ (int) setClipBoard:(NSDictionary*)dict
{
NSLog(@"setClipBoard");
NSString *text = [dict objectForKey:@"text"];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = text;
//[[ZFToast shareClient] popUpToastWithMessage:@"复制成功"];
return 1;
}
+ (NSString*) getClipBoard
{
NSLog(@"getClipBoard");
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
return pasteboard.string;
}
上一篇: 求php从一段字符串中截取指定内容的正则表达式代码解决方案
下一篇: cocos2dx简单切换场景