WebView usage vs. js interaction (2) in response to a picture click event in WebView

Source: Internet
Author: User

Reference: http://blog.csdn.net/wangtingshuai/article/details/8635787

JS Code

Inject JS function to listen to private void Addimageclicklistner () {//This function of JS function is to traverse all the IMG points, and add the onclick function, The function is to invoke the local Java interface when the image is clicked and pass the URL past Contentwebview.loadurl ("javascript: (function () {" + "var Objs = document.getElementsByTagName (\ "img\"); "+" for (var i=0;i<objs.length;i++)  "+" {"+"    objs[i].onclick=function ()  "+"    {  "+"        Window.imagelistner.openImage (THIS.SRC);  " + "    }  " + "}" + "})()");}

Filter pictures

    //inject JS function to monitor    Private voidAddimageclicklistner () {//The function of this JS function is to traverse all of the IMG, and add the OnClick function, function is to call the local Java interface when the image is clicked and pass the URL pastContentwebview.loadurl ("javascript: (function () {" + "var objs = document.getElementsByTagName (\" img\ ");" + "for (Var i=0;i<objs.length;i++)" + "{" + "if (OBJS[I].SRC)"                +"if (objs[i].src.indexof (' Display ') >0)"+" objs[i].onclick=function () {"+" window.imagelistner.openImage (THIS.SRC); "                + "    }  "                 + "}"                 + "})()"); }

  

Full code

 Packagecom.example.testandroid;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.util.Log;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient; Public classMainactivityextendsactionbaractivity {WebView contentwebview; @SuppressLint ("Javascriptinterface") @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Contentwebview=(WebView) Findviewbyid (R.id.webview); //Enable JavaScriptContentwebview.getsettings (). setjavascriptenabled (true); //Just looking for a website with a picture.Contentwebview.loadurl ("http://www.qdaily.com/display/articles/7237"); //Add the JS interface class and Alias ImagelistnerContentwebview.addjavascriptinterface (NewJavascriptinterface ( This),                "Imagelistner"); Contentwebview.setwebviewclient (Newmywebviewclient ()); }    //inject JS function to monitor    Private voidAddimageclicklistner () {//The function of this JS function is to traverse all of the IMG, and add the OnClick function, function is to call the local Java interface when the image is clicked and pass the URL pastContentwebview.loadurl ("javascript: (function () {" + "var objs = document.getElementsByTagName (\" img\ ");" + "for (Var i=0;i<objs.length;i++)" + "{"//+ "Window.imagelistner.openImage (objs[i].src)"+ "objs[i].onclick=function ()"+"{"+"window.imagelistner.openimage(this.src); "                + "    }  "                 + "}"                 + "})()"); }    //JS Communication Interface     Public classJavascriptinterface {Privatecontext Context;  PublicJavascriptinterface (Context context) { This. Context =context; }         Public voidopenimage (String img) {LOG.E ("MyTag", IMG); }                         Public voidlog (String s) {LOG.E ("MyTag", s); }    }    //Monitor    Private classMywebviewclientextendswebviewclient {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {return Super. shouldoverrideurlloading (view, URL); } @Override Public voidonpagefinished (WebView view, String URL) {view.getsettings (). setjavascriptenabled (true); Super. onpagefinished (view, URL); //after the HTML loading is complete, add the Click JS function of the listening image.Addimageclicklistner (); } @Override Public voidonpagestarted (WebView view, String URL, Bitmap favicon) {view.getsettings (). setjavascriptenabled (true); Super. onpagestarted (view, URL, favicon); } @Override Public voidOnreceivederror (WebView view,intErrorCode, string description, String failingurl) {            Super. Onreceivederror (view, ErrorCode, description, Failingurl); }    }}
Let's do the pictures in Openimage.

WebView usage vs. js interaction (2) in response to a picture click event in WebView

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.