WebView API Detailed

Source: Internet
Author: User
Tags int size

1.WebView

When you use the WebView control, you first need to define a WebView control in the XML layout file, as follows:

1 <webview 
2    android:id= "@+id/webview"
3    android:layout_width= "Match_parent"
4    Andro id:layout_height= "Match_parent"    />

There are a number of methods available in WebView, for example, we can use the CanGoBack () method to determine whether you can return a previous open page from the page, use the GetTitle () and Geturl () methods to get the title and URL path of the current page, and use Loadurl ( String URL) method to load the Web page you want to open, and so on. The following code opens the Baidu home page in the WebView control by using the Loadurl () method.

1 private webview Mwebview;
2 Mwebview = (webview) This.findviewbyid (R.id.webview);
3 Mwebview.loadurl ("http://www.baidu.com/");

2.WebSettings

WebSettings is used to set the properties and state of the webview. WebSettings and WebView exist in the same lifecycle, and the WebSettings object can be obtained using the following methods.

WebSettings websettings = Mwebview.getsettings ();

When the WebView is created, the system defaults to WebView, and when we get the WebSettings object from the above method, we can remove the WebView default properties and state from the WebSettings object, of course, We can also set the default properties and state of the WebView by WebSettings objects.

WebSettings provides some common ways to set webview properties and states as follows:

(1) setallowfileaccess (Boolean allow); Set enable or disable access to file data

(2) Setbuiltinzoomcontrols (Boolean enabled); Set whether scaling is supported

(3) setdefaultfontsize (int size); Set the default font size

(4) setjavascriptenabled (Boolean flag); Setting whether JavaScript is supported

(5) Setsupportzoom (Boolean support); Set whether Zoom is supported

3.WebViewClient

Webviewclient is mainly used to assist WebView to handle various notices, requests and other events. We can specify a webviewclient for the WebView object by means of the WebView setwebviewclient () method, which is implemented as follows:

1 mywebviewclient mywebviewclient = new Mywebviewclient ();
 2 mwebview.setwebviewclient (mywebviewclient);
 3
 4/*
 5      * class:mywebviewclient, for auxiliary webview, handling various notices, requests and other events
 6      * Author:    Blog Park-still indifferent
 7      */
 8     Private class Mywebviewclient extends Webviewclient {
 9         //Rewrite the parent class method, Let the newly opened page appear in the current WebView         boolean shouldoverrideurlloading (webview view, String URL) {             View.loadurl (URL);
return             true;
}     

As you can see, in the code above, we implemented the Shouldoverrideurlloading () method of the parent class webviewclient in the subclass mywebviewclient to display the newly opened Web page in the current webview. Instead of calling the Android system's own browser to access it.

There are also a number of methods available in webviewclient, such as the following:

(1) doupdatevisitedhistory (webview view, String URL, boolean isreload); Update history

(2) onformresubmission (webview view, message dontresend, message resend); Re-request Web page data

(3) Onloadresource (webview view, String URL); Loads the resources provided by the specified URL

(4) onpagefinished (webview view, String URL); Web page Loading complete

(5) onpagestarted (webview view, String URL, Bitmap favicon); Web page starts loading

(6) Onreceivederror (webview view, int errorcode, string description, String failingurl); Reporting error messages

4.WebChromeClient

Webchromeclient is primarily used to assist WebView with JavaScript dialog boxes, site icons, site titles, and page loading schedules.

Similarly, we can specify a webchromeclient for the WebView object through the WebView setwebchromeclient () method.

In Webchromeclient, the onprogresschanged (webview view, int newprogress) method is invoked when the loading progress of a Web page is changed When the icon for the Web page changes, the Onreceivedicon (Webview view, bitmap icon) method is invoked, and when the title of the page changes, Onreceivedtitle ( Webview view, string title) method is invoked. With these methods, we can easily get the loading progress of the Web page, the title and icon of the page, and so on, as shown in the following code:

1     mywebchromeclient mywebchromeclient = new Mywebchromeclient ();
 2     mwebview.setwebchromeclient (mywebchromeclient);
 3     
 4
 /* 5      * Class    :   for auxiliary WebView, Processing JavaScript dialog box, Web site icon, site title, load progress, etc.
 6      * Author   :   Blog Park-still indifferent
 7      /
 8     Private class Mywebchromeclient extends Webchromeclient {
 9         
10         //Get the loading progress of the Web page, displayed in the TextView control in the upper-right corner, public         void onprogresschanged (webview view, int newprogress) {             if (newprogress < MB) {                 String progress = newprogress + "%";                 Mtextview_progress.settext (progress);             } else {                 mtextview_progress.settext ("");
The             title of the         
Web page is displayed as the title of the application to display the public         void Onreceivedtitle ( WebView view, String title) {             MainActivity.this.setTitle (title);     }

5.WebView and JavaScript

Not only can you run HTML code in WebView, but more importantly, WebView can invoke each other with JavaScript. That is, you can get the content of WebView in JavaScript, while in WebView you can call methods in JavaScript.

Here's how to invoke the method inside the JavaScript in WebView.

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

1     /*********************************/
2/     *           Find Place            * *
3     /**************************** /
4     var city = new Bmap.localsearch (map,{renderoptions:{map:map,autoviewport:true});   
5     
6     function findplace (place)
7     {
8         city.search (place);
9}

All we have to do is call the Findplace () method in WebView to complete the location lookup. The way in which JavaScript is invoked in WebView is achieved by code Webview.loadurl ("javascript: Method Name ()"). As the code below, get the name of the place the user wants to find from the EditText control, and then call the Findplace () method in JavaScript to find it.

 1 * 2 * Function: Click event Handling 3 * Author: Blog Park-Still cool 

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.