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:
- Modify "Loading ..." to "load complete"
- Change the loading progress bar loading.gif to the Dragon cat picture dragon.jpg
- "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.
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. png2. 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("您好,我是龙猫军团!");
Get WebView loaded Web page content and make dynamic changes