本文介绍: <WebView android:id=”@+id/webviewandroid:layout_width=”fill_parentandroid:layout_height=”wrap_content” />初始化这里读的是文本 @SuppressLint(“JavascriptInt…
                <WebView
                    android:id="@+id/webview"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    /&gt;

初始化这里读的是文本

  @SuppressLint("JavascriptInterface")
    private void initWebView(String html) {
        // 启用javascript
        contentWebView.getSettings().setJavaScriptEnabled(true);
        contentWebView.getSettings().setDefaultTextEncodingName("UTF-8");
//        // 随便找了个带图片网站
//        contentWebView.loadUrl("https://www.pmcaff.com/discuss/3394893931276352?newwindow=1");
        contentWebView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);//加载html数据

        // 添加js交互接口类,并起别名 imagelistner
//        contentWebView.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");

        contentWebView.addJavascriptInterface(new JavascriptInterface2(this), "imageListener");

        contentWebView.setWebViewClient(new MyWebViewClient());

    }
// 监听
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            view.getSettings().setJavaScriptEnabled(true);
            addImageClickListner2();
            super.onPageFinished(view, url);
            // html加载完成之后,添加监听图片点击js函数
//            addImageClickListner();


        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            view.getSettings().setJavaScriptEnabled(true);

            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            super.onReceivedError(view, errorCode, description, failingUrl);

        }
    }
// 注入js函数监听
    private void addImageClickListner2() {
        //网页加载完成 走JS代码
        contentWebView.loadUrl("javascript:(function(){" +
                "var objs = document.getElementsByTagName("img"); " +
                "var imgUrl = "";"+
                "var filter = ["img//EventHead.png","img//kong.png","hdtz//button.png"];"+
                "var isShow = true;"+
                "for(var i=0;i<objs.length;i++){" +
                "for(var j=0;j<filter.length;j++){"+
                "if(objs[i].src.indexOf(filter[j])>=0) {"+
                "isShow = false; break;}}"+
                "if(isShow &amp;&amp; objs[i].width>1){"+
                "imgUrl += objs[i].src + ',';isShow = true;"+
                "    objs[i].οnclick=function()  " +
                "    {  "
                + "        window.imageListener.openImage(imgUrl,this.src);" +
                "    }" +
                "}" +
                "}" +
                "})()"
        );

    }
  // js通信接口
    public class JavascriptInterface2 {

        private Context context;

        public JavascriptInterface2(Context context) {
            this.context = context;
        }

        @android.webkit.JavascriptInterface
        public void openImage(String imageUrl,String img) {
            int position = 0;
            String[] imgs = imageUrl.split(",");
            ArrayList<String> imgUrlList = new ArrayList<>();
            for (String s : imgs) {
                imgUrlList.add(s);
            }
            for (int i = 0;i<imgUrlList.size();i++){
                if (img.equals(imgUrlList.get(i))){
                    position = i;
                }
            }
            ImageHandlerUtil.browseImages(getContext(), imgUrlList, position);

//            Intent intent = new Intent(MainActivity.this, ImageActivity.class);
//            intent.putExtra("number",position);
//            intent.putExtra("list",(Serializable) imgUrlList);
//            startActivity(intent);
        }

    }

原文地址:https://blog.csdn.net/iromkoear/article/details/124750709

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_7331.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注