Get WebView loaded Web page content and make dynamic changes

Source: Internet
Author: User

Http://www.jianshu.com/p/3f207a8e32cb

"Android" webview read local picture http://www.cnblogs.com/kimmy/p/4769788.html get webview loaded Web page content and make dynamic changes

Afinalstone2016.10.11 18:56* words 587


, the entire interface has only one WebView control, When the program initializes, the WebView will load the jsoupparhtml.html file under the Assets folder, and when WebView successfully loads the HTML page, the program will again get the specific contents of the HTML that the WebView control is currently loading, wait 5 seconds, move through the jsoup frame, Three element and modify its properties as follows:

    1. Modify "Loading ..." to "load complete"
    2. Change the loading progress bar loading.gif to the Dragon cat picture dragon.jpg
    3. "Hello, I am the progress bar" modified to "Hello, I am the Dragon Cat Corps"

The following is a detailed HTML file and Java code:

    • jsoupparhtml.html files under Assets

HTML file is very simple, the main content is two paragraphs of text and a picture.

<! DOCTYPE html>
    • Activity-corresponding Java code
Import Android.graphics.bitmap;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.util.log;import Android.webkit.javascriptinterface;import Android.webkit.webview;import Android.webkit.webviewclient;import Org.jsoup.jsoup;import Org.jsoup.nodes.document;import Org.jsoup.nodes.element;import Org.jsoup.select.elements;public class Jsouphtmlactivity extends AppCompatActivity {/ The jsoupparhtml.html of the file under/assets is located in the absolute path private static final String Default_url = "file:///android_asset/JsoupParHtml.    HTML ";    WebView WebView;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_jsoup_html);        WebView = (WebView) Findviewbyid (R.id.webview);    InitData (); } private void InitData () {//The following three lines are set primarily for WebView to successfully load HTML pages, we are able to obtain specific HTML strings through WebView Webview.getsetti        NGS (). Setjavascriptenabled (True); Webview.addjavascriptinterfaCE (New Injavascriptlocalobj (), "local_obj"); Webview.setwebviewclient (New Webviewclient () {@Override public void onpagestarted (WebView view, STR            ing URL, Bitmap favicon) {super.onpagestarted (view, URL, favicon); } @Override public Boolean shouldoverrideurlloading (WebView view, String URL) {view.                Loadurl (URL);            return true; } @Override public void onpagefinished (WebView view, String URL) {super.onpagefinish                ed (view, URL); View.loadurl ("Javascript:window.local_obj.showSource (' 

The code is divided into two main parts
The first part: by adding the JS listener object to get to the WebView already loaded HTML content, will be in the Injavascriptlocalobj Showsource callback method to get to webview the current loading of the successful interface content.
The second part: through Jsoup the acquired HTML content into the Document object, and then get to the elements we need, and its corresponding properties set, we have achieved the effect.

    • Simple use of Jsoup

The code uses three methods Getelementsbyclass (), getElementById (), select () to get a specific element, and there are many more ways to get it, and through Html,attr, Text three methods to modify the existing properties, below the specific points.

1. Some ways to get elements:
There is a select method that is commonly used, and many others, as shown in


Gets the method. png

2. Property settings

//获取所有类名为comments并还有a元素的div并设置其ref属性doc.select("div.comments a").attr("rel", "nofollow"); //获取所有类名为masthead的div设置它们的title属性并添加css class属性doc.select("div.masthead").attr("title", "jsoup").addClass("round-box");

For the above code, you can analogy this code in the program:

element.attr("src","file:///android_asset/dragon.jpg");

3.HTML settings

Element div = doc.select("div").first(); // 现在:<div></div>div.html("<p>lorem ipsum</p>"); // 现在:<div><p>lorem ipsum</p></div>div.prepend("<p>First</p>");div.append("<p>Last</p>");// 最后: <div><p>First</p><p>lorem ipsum</p><p>Last</p></div>Element span = doc.select("span").first(); // 现在:<span>One</span>span.wrap("<li><a href=‘http://example.com/‘></a></li>");// 最后: <li><a href="http://example.com"><span>One</span></a></li>

For the above code, you can analogy this code in the program:

elements.get(0).html("<p>加载完成</p>");

4. Text Settings

Element div = doc.select("div").first(); // 现在:<div></div>div.text("five > four"); // 现在:<div>five > four</div>div.prepend("First ");div.append(" Last");// 最后: <div>First five > four Last</div>

For the above code, you can analogy this code in the program:

elements.get(0).text("您好,我是龙猫军团!");
    • About the specific use of Jsoup
      See all the information and details listed in the Jsoup Development Guide in the Chinese user manual and Selectorapi resources

      Finally attached demo

Get WebView loaded Web page content and make dynamic changes

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.