Android study notes 50: Use the webview control to browse webpages

Source: Internet
Author: User

In Android, you can use the webview control to browse webpages. By using this control, we can make a simple browser, as shown in figure 1.

Figure 1 Running Effect

 

1. webview

When using the webview control, you must first define a webview control in the XML layout file. The method is as follows:

1<Webview2Android: ID= "@ + ID/webview"3 Android: layout_width= "Match_parent"4 Android: layout_height= "Match_parent"/>

Webview provides many methods. For example, we can use the cangoback () method to determine whether the previous opened webpage can be returned from the webpage; Use gettitle () and geturl () method to obtain the title and URL path of the current webpage; Use loadurl (string URL) to load the webpage to be opened, and so on. The followingCodeYou can use the loadurl () method to open the Baidu homepage in the webview control.

1PrivateWebview mwebview;2Mwebview = (webview)This. Findviewbyid (R. Id. webview );3Mwebview. loadurl ("http://www.baidu.com /");

 

 2. websettings

Websettings is used to set the attributes and status of webview. Websettings and webview exist in the same lifecycle. You can use the following method to obtain the websettings object.

 
Websettings = mwebview. getsettings ();

When creating a webview, the system will make some default settings for the webview. After we get the websettings object through the above method, we can retrieve the default attributes and status of the webview from the websettings object, of course, we can also set the default attributes and status of webview through the websettings object.

Websettings provides some common methods to set the attributes and status of webview:

(1) setallowfileaccess (Boolean allow); // you can enable or disable access to file data.

(2) setbuiltinzoomcontrols (Boolean enabled); // you can specify whether scaling is supported.

(3) setdefaultfontsize (INT size); // you can specify the default font size.

(4) setjavascriptenabled (Boolean flag); // you can specify whether Javascript is supported.

(5) setsupportzoom (Boolean support); // you can specify whether Zoom is supported.

 

3. webviewclient

Webviewclient is used to assist webview in handling various notifications, requests, and other events. You can use the setwebviewclient () method of webview to specify a webviewclient for the webview object. The specific implementation method is as follows:

 1 Mywebviewclient = New  Mywebviewclient ();  2   Mwebview. setwebviewclient (mywebviewclient );  3  4       /*  5  * Class: mywebviewclient, used to assist webview and process various notifications, requests, and other events  6   * Author: blog Park-still indifferent  7        */  8       Private   Class Mywebviewclient Extends  Webviewclient {  9           10           //  Override the parent method to display the newly opened webpage in the current webview.  11          Public   Boolean  Shouldoverrideurlloading (webview view, string URL ){  12   View. loadurl (URL );  13               Return   True  ;  14   }  15           16 }

As you can see, in the above Code, the shouldoverrideurlloading () method of the parent class webviewclient is rewritten in the subclass mywebviewclient to display the newly opened web page in the current webview, instead of calling the browser provided by the Android system for access.

Webviewclient also provides many methods, such as the following:

(1) doupdatevisitedhistory (webview view, string URL, Boolean isreload); // update history

(2) onformresubmission (webview view, message dontresend, message resend); // re-request webpage data

(3) onloadresource (webview view, string URL); // load resources provided by the specified URL

(4) onpagefinished (webview view, string URL); // The webpage has been loaded.

(5) onpagestarted (webview view, string URL, bitmap favicon); // start loading the webpage.

(6) onreceivederror (webview view, int errorcode, string description, string failingurl); // report error information

 

4. webchromeclient

Webchromeclient is used to assist webview in processing Javascript dialogs, website icons, website titles, and webpage loading progress.

Similarly, we can use the setwebchromeclient () method of webview to specify a webchromeclient for the webview object.

In webchromeclient, The onprogresschanged (webview view, int newprogress) method is called when the webpage loading progress changes. When the webpage icon changes, onreceivedicon (webview view, bitmap icon) method is called. When the webpage title changes, the onreceivedtitle (webview, String title) method is called. By using these methods, we can easily obtain information such as the webpage loading progress, webpage title, and icon, as shown in the following code:

 1 Mywebchromeclient = New  Mywebchromeclient ();  2   Mwebview. setwebchromeclient (mywebchromeclient );  3       4  /*  5  * Class: Used to assist webview and process JavaScript dialogs, website icons, website titles, loading progress, etc.  6   * Author: blog Park-still indifferent  7        */  8       Private   Class Mywebchromeclient Extends  Webchromeclient {  9           10           //  Obtain the page loading progress, which is displayed in the textview control in the upper right corner.  11          Public   Void Onprogresschanged (webview view, Int  Newprogress ){  12               If (Newprogress <100 ){  13 String progress = newprogress + "%" ;  14   Mtextview_progress.settext (Progress );  15 } Else  { 16 Mtextview_progress.settext ("" );  17   }  18   }  19           20           //  Obtain the webpage title as an applicationProgramIS DISPLAYED  21           Public   Void  Onreceivedtitle (webview view, String title ){  22 Mainactivity. This  . Settitle (title );  23   }  24           25 }

 

5. webview and JavaScript

In webview, not only HTML code can be run, but more importantly, webview can be called with JavaScript. That is to say, you can get the content of webview in Javascript. At the same time, you can also call the methods in Javascript in webview.

The following describes how to call methods in Javascript in webview.

Here, I use the Baidu map API (an HTML document embedded with JavaScript), which provides the following JavaScript method:

1       /*  *******************************  */  2       /*  Locate  */  3       /*  *******************************  */  4       VaR City = New Bmap. localsearch (MAP, {renderoptions: {map: map, autoviewport:True  }});  5       6       Function  Findplace (place)  7   {  8   City. Search (place );  9 }

What we need to do is to call the findplace () method in webview to complete the location search. In webview, the method in Javascript is called through the Code webview. loadurl ("javascript: Method Name. The following code obtains the place name you want to search for from the edittext control, and then calls the findplace () method in Javascript for search.

 1      /*  2   * Function: Click Event processing.  3   * Author: blog Park-still indifferent  4        */  5       Public   Void  Onclick (view ){  6           Switch  (View. GETID ()){  7           Case R. Id. imagebutton_search: //  Search for place names  8 String STR = Medittext_input.gettext (). tostring ();  9 String url = "javascript: findplace ('" + STR + "')" ;  10   Mwebview. loadurl (URL );  11               Break  ;  12   }  13 }

For example, if we search for "Yuan mingyuan", we can see the search result shown in 2. 

 

Figure 2 search for "Yuan mingyuan"

 

 

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.