当前位置:编程学习 > wap >>

在cocos2d-x界面中嵌入Android的WebView

在Cocos2dxActivity.java中,
(1) 增加函数onCreateLayout,
[java
 
    public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView surfaceView) {  
        LinearLayout layout = new LinearLayout(this);  
        layout.setOrientation(LinearLayout.VERTICAL);  
        layout.addView(surfaceView);  
        return layout;  
    }  
 
(2) 在 this.mGLSurfaceView = this.onCreateView() 下面增加这一行:
[java]
 
LinearLayout contentLayout = this.onCreateLayout(mGLSurfaceView);  
 
(3) 应用的Activity文件实现如下,
[java] 
 
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); &nbs
补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,