CocosCreator中JS调用Android平台实现复制功能

在cocos2dx-js中先在判断是否是手机端isMobile,手机端包括手机网页isNative和手机原生端cc.sys.OS_IOS 、 cc.sys.OS_ANDROID,web端isBrowser

在cocos项目中需要点击复制的地方写上方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
touchCopy(){
let text = this.label.string;
if(cc.sys.isMobile){
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "copyTextFuc", "(Ljava/lang/String;)V",text);

}else if(cc.sys.isBrowser){

const el = document.createElement('textarea');
el.value = text;
el.setAttribute('readonly', '');
el.style.contain = 'strict';
el.style.position = 'absolute';
el.style.left = '-9999px';
el.style.fontSize = '12pt'; // Prevent zooming on iOS
const selection = getSelection();
var originalRange = false;
if (selection.rangeCount > 0) {
originalRange = selection.getRangeAt(0);
}
document.body.appendChild(el);
el.select();
el.selectionStart = 0;
el.selectionEnd = input.length;
var success = false;
try {
success = document.execCommand('copy');
} catch (error) {
document.body.removeChild(el);
if (originalRange) {
selection.removeAllRanges();
selection.addRange(originalRange);
}
}
}
}

关于 jsb.reflection.callStaticMethod(className, methodName, methodSignature, parameters…) 方法中参数介绍:

  • 第一个参:第一个为打包过后需要调用的类的所在文件下的包名+类名。
  • 第二个参:方法名,在该类下的方法名字需公共方法 public static
  • 第三个参:(Ljava/lang/String;)V代表带一个字符串参数没有返回值的方法函数。
    • 例一: (II)V 代表含有两个int类型的没有返回值的方法函数。
    • 例二:(Ljava/lang/String;Ljava/lang/String;)I 代表含有两个字符串参数的返回值为int类型的方法函数。
    • 例三:(FI)Z 代表含有一个float和一个int,返回值为boolean的方法函数。
java类型 签名
int I
float F
boolean Z
String Ljava/lang/String;

用Android Studio打开项目
在AppActivity中写入上面的回调方法函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

public class AppActivity extends Cocos2dxActivity {

private static AppActivity app;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
// Don't need to finish it again since it's finished in super.onCreate .
return;
}
// DO OTHER INITIALIZATION BELOW
SDKWrapper.getInstance().init(this);
app = this;
}

public static void copyTextFuc(final String message){
app.showAlertDialog(message);
//获取剪贴板管理器:
ClipboardManager cm = (ClipboardManager) app.getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
ClipData mClipData = ClipData.newPlainText("Label", message);
//将ClipData内容放到系统剪贴板里。
cm.setPrimaryClip(mClipData);
}

public static void showAlertDialog(final String message) {

//使用runOnUiThread
app.runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog alertDialog = new AlertDialog.Builder(app).create();
alertDialog.setMessage(message);
alertDialog.setIcon(R.drawable.ic_dialog_alert);
alertDialog.show();
}
});
}

如果是mac端的Android studio 直接使用option+enter 快捷导入使用到的包名。

import android.R;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;

然后打包测试 应该就可以复制粘贴了。