Detailed explanation of the method of using WebView control to browse web pages in Android Development

This article describes the method of using WebView control to browse web pages in Android development. Share with you for your reference, as follows:

There is a problem of mathematical display in the project. The conventional textview display can not handle the mathematical formula, and the use of image generation has a great pressure on the server. After query, it can be realized by loading JS through WebView. The same method of IOS can be implemented, but the rendering efficiency of JS is much higher than that of Android. Summarize WebView.

1.WebView

When using a WebView control, you first need to define a WebView control in the XML layout file. The definition method is as follows:

WebView provides many methods. For example, we can use the cangoback () method to determine whether we can return to the last open web page from this web page; Use GetTitle () and geturl () methods to obtain the title and URL path of the current web page; Use the loadurl (string URL) method to load the web page to be opened, and so on. The following code opens the baidu home page in the WebView control by using the loadurl () method.

2.WebSettings

Websettings is used to set the properties and status of WebView. Websettings and WebView exist in the same life cycle. You can use the following methods to obtain the websettings object.

When creating WebView, the system will make some default settings for WebView. After we get the websettings object through the above methods, we can get the default properties and status of WebView from the websettings object. Of course, we can also set the default properties and status of WebView through the websettings object.

Some common methods provided by websettings to set the properties and status of WebView are 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) set JavaScript enabled (boolean flag)// Set whether JavaScript is supported (5) setsupportzoom (Boolean support)// Sets whether zoom is supported

3.WebViewClient

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

As you can see, in the above code, by overriding the shouldoverrideurlloading() method of the parent webviewclient in the subclass mywebviewclient, we can enable the newly opened web page to be displayed in the current WebView instead of calling the browser of the Android system.

Webviewclient also provides many methods, such as the following:

(1)doUpdateVisitedHistory(WebView view,String url,boolean isReload); // Update history (2) onformresubmission (WebView, message dontrend, message resend)// Re request web page data (3) onloadresource (WebView, string URL)// Load the resources provided by the specified web address (4) onpagefinished (WebView, string URL)// After the web page is loaded, the JS rendering we do is carried out after onpagefinish. The more complex JS is, the slower the rendering is. (5)onPageStarted(WebView view,Bitmap favicon);// The web page starts loading (6) onreceivederror (WebView, int errorcode, string description, string failingurl)// Report error messages

It is mainly to call the display after the web page fails to load.

4.WebChromeClient

Webchromeclient is mainly used to assist WebView in handling JavaScript dialog boxes, website icons, website titles, web page loading progress, etc.

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

In the webchromeclient, when the loading progress of the web page changes, the onprogresschanged (WebView, int newprogress) method will be called; When the icon of the web page changes, onreceiveicon (WebView, bitmap icon) method will be called; When the title of a web page changes, the onreceivedtitle (WebView, string title) method will be called. Using these methods, we can easily obtain the information such as the loading progress of the web page, the title and icon of the web page, as shown in the following code:

5. WebView and JavaScript

In WebView, you can not only run HTML code, but also call each other with JavaScript. In other words, the content of WebView can be obtained in JavaScript. At the same time, the methods in JavaScript can also be called in WebView.

Now let's talk about how to invoke the method in Javascript in WebView.

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

What we need to do is to call the findPlace () method in WebView to complete the location lookup. The method of calling Javascript in WebView is implemented by code WebView.loadUrl ("javascript: method name ()"). The following code gets the place name that the user wants to find from the EditText control, and then calls the findPlace () method in Javascript to find it.

6 acceleration of WebView loading JS

Load string

WebView loading pictures

Load HTML under assets

More readers interested in Android related content can view the special topics of this site: summary of Android view skills, summary of activity operation skills of Android programming, summary of Android layout skills, introduction and advanced tutorial of Android development, summary of Android resource operation skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>