在Cocos2dxActivity.java中,
(1) 增加函数onCreateLayout,
- public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView surfaceView) {
- LinearLayout layout = new LinearLayout(this);
- layout.setOrientation(LinearLayout.VERTICAL);
- layout.addView(surfaceView);
- return layout;
- }
(2) 在 this.mGLSurfaceView = this.onCreateView() 下面增加这一行:
- LinearLayout contentLayout = this.onCreateLayout(mGLSurfaceView);
(3) 应用的Activity文件实现如下,
- public class HelloCpp extends Cocos2dxActivity{
- static HelloCpp sHelloCpp = null;
- LinearLayout mContentLayout;
- Cocos2dxGLSurfaceView mGlSurfaceView;
- LinearLayout mWebLayout;
- WebView mWebView;
- Button mBackButton;
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- }
- public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView surfaceView) {
- mGlSurfaceView = surfaceView;
- sHelloCpp = this;
- mContentLayout = new LinearLayout(this);
- mContentLayout.setOrientation(LinearLayout.VERTICAL);
- mContentLayout.addView(surfaceView);
- mWebLayout = new LinearLayout(this);
- mWebLayout.setOrientation(LinearLayout.VERTICAL);
- return mContentLayout;
- }
- public Cocos2dxGLSurfaceView onCreateView() {
- Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
- // TestCpp should create stencil buffer
- glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
- return glSurfaceView;
- }
- //此函数提供给jni调用,返回自身类的对象
- public static HelloCpp getInstance() {//返回实例
- return sHelloCpp;
- }
- public void openWebView() {
- this.runOnUiThread(new Runnable() {//在主线程里添加别的控件
- public void run() {
- //初始化webView
- mWebView = new WebView(HelloCpp.this);
- //设置webView能够执行javascript脚本
- mWebView.getSettings().setJavaScriptEnabled(true);
- //载入URL
- mWebView.loadUrl("file:///android_asset/index.html");
- //使页面获得焦点
- //mWebView.requestFocus();
- //如果页面中链接,如果希望点击链接继续在当前browser中响应
- mWebView.setWebViewClient(new WebViewClient(){
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- if(url.indexOf("tel:")<0){
- view.loadUrl(url);
- }
- return true;
- }
- });
- /*初始化返回按钮*/
- mBackButton = new Button(HelloCpp.this);
- mBackButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
- mBackButton.setText("Close");
- mBackButton.setTextColor(Color.argb(255, 255, 218, 154));
- mBackButton.setTextSize(14);
- mBackButton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- removeWebView();
- mGlSurfaceView.setVisibility(View.VISIBLE);
- }
- });
- //把webView加入到线性布局
- mGlSurfaceView.setVisibility(View.GONE);
- mWebLayout.addView(mBackButton);
- mWebLayout.addView(mWebView);
- mContentLayout.addView(mWebLayout);
- }
- });
- }
- //移除webView 把刚才加的所有控件都删掉
- public void removeWebView() {
- mContentLayout.removeView(mWebLayout);
- mWebLayout.destroyDrawingCache();
- mWebLayout.removeView(mWebView);
- mWebView.destroy();
- mWebLayout.removeView(mBackButton);
- mBackButton.destroyDrawingCache();
- }
- public boolean onKeyDown(int keyCoder,KeyEvent event) //重载函数,android手机实体返回键回调函数
- {
- if(mWebView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK){//如果网页能回退则后退,如果不能后退移除WebView
- mWebView.goBack();
- }else{
- removeWebView();
- mGlSurfaceView.setVisibility(View.VISIBLE);
- }
- return false;
- }
- static {
- System.loadLibrary("game");
- }
从cocos2d-x的界面中打开WebView的代码:
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- //getStaticMethodInfo,判断Java静态函数是否存在,并且把信息保存到minfo里
- //参数1:JniMethodInfo
- //参数2:Java类包名+类名
- //参数3:Java函数名称
- //参数4:函数参数类型和返回值类型,这里的返回值类型是HelloCpp类的对象。写法:L+包名+; 其他的类型请看上面的“JNI详细教程”
- JniMethodInfo minfo;
- jobject jobj;
- bool isHave = JniHelper::getStaticMethodInfo(minfo, "cn/livelog/popdiamond/HelloCpp","getInstance","()Lcn/livelog/popdiamond/HelloCpp;");
- if (isHave)
- {
- //调用Java静态函数,取得对象。
- jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);
- if (jobj != NULL)
- {
- isHave = JniHelper::getMethodInfo(minfo,"cn/livelog/popdiamond/HelloCpp","openWebView","()V");
- if (isHave)
- {
- //调用java非静态函数, 参数1:Java对象,上面已经取得 参数2:方法ID
- minfo.env->CallVoidMethod(jobj, minfo.methodID);
- }
- }
- }
- #endif