关于原生+WebView js交互、数据传输问题

 

原生能接收到webView内控件点击事件,以至拿到点击事件传过来数据 

 

1、定义一个接口

public interface WebHomeOnClickInterface {
    void showStorePicker();        //显示选择框的事件
    void cancelStorePicker();      //选择框消失的事件
}

 

2、定义一个类使用该接口

public class JsInterationHome {

    WebHomeOnClickInterface webOnClickInterface;

    @JavascriptInterface
    public void showStorePicker(){
        webOnClickInterface.showStorePicker();
    }
    @JavascriptInterface
    public void cancelStorePicker(){
        webOnClickInterface.cancelStorePicker();
    }


}

 

3、用到WebView的页面使用

(1)、activity或者fragment继承该接口

implements  WebHomeOnClickInterface {
}

(2)、webView控件的初始化

WebSettings settings = webView.getSettings();
        settings.setUserAgentString(settings.getUserAgentString().replace("Android", "Android"));
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setUseWideViewPort(false);  //将图片调整到适合webview的大小
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(false); // 支持缩放
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局
        settings.setDisplayZoomControls(false); //隐藏webview缩放按钮
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(false); // 显示放大缩小 controler
        settings.setDomStorageEnabled(true);
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        settings.setTextZoom(100);
        settings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);// 默认缩放模式
        settings.setLoadsImagesAutomatically(true);
     // 重点!!! 重点!!!
        settings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new JsInterationHome(this), "control");//js 交互

 

 同时设置webView.setWebViewClient();

webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                // 页面加载完成后注册js
                initJs(view);
                super.onPageFinished(view, url);

            }
            // 其他若干方法隐藏
        });        

 

 // 具体注解代码

private void initJs(WebView view) {
        // 判断是否主已经注解js,做过的话不要再次注解js
        if (!isHasInitJs) {
            isHasInitJs = true;
        } else {
            return;
        }
        view.loadUrl("javascript:\n" +
                "window.addEventListener('route', function(e) {\n" +
                "  control.interceptUrl(e.detail.pathname, e.detail.href);\n" +
                "})"
        );
        // 监听保存按钮 ,获取返回值 字符串    把对象转字符串 android再解析
        
       view.evaluateJavascript("javascript:window.addEventListener('showStorePicker',function(e){control.showStorePicker()})", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        Log.i("xqxinfo", "showStorePicker");
                    }
                }
        );
        view.evaluateJavascript("javascript:window.addEventListener('cancelStorePicker',function(e){control.cancelStorePicker()})", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        Log.i("xqxinfo", "cancelStorePicker");
                    }
                }
        );

    }        

 

 原生收到web发送的通知后的处理

 @Override
    public void showStorePicker(){
       
    }

    @Override
    public void cancelStorePicker() {

    }

 

 

 

  

posted @ 2021-10-21 12:07  听着music睡  阅读(555)  评论(0编辑  收藏  举报