Android WebView application interface development tutorial

The WebView component itself is a browser implementation, android5 0 enhanced WebView is based on chromium M37 and directly supports webrtc, webaudio and webgl. Developers can use polymer and material design directly in WebView.

I WebView browse web pages (load online URL)

WebView provides many methods to perform browser operations. Common methods are as follows:

Void goback(): backward void goforward(): forward. Void gobackorforforward (int step): if step is positive, it means forward; if step is negative, it means backward. Void loadurl (string URL): loads the web page corresponding to the specified URL. Boolean zoomin(): zoom in on the web page. Boolean zoom out(): zoom out the web page.

Example: Mini browser

The example contains two interfaces. The first interface includes inputting and opening web addresses. The second interface includes a WebView, which is used to display the interface corresponding to the URL entered in the first interface. The program code is as follows:

First interface

This interface is very simple. An input box and a button are used to enter the website address and open WebView respectively.

Second interface

This code calls the loadurl (string URL) method of WebView to load and display the web page corresponding to the URL, and set it to support JavaScript. If you jump to the browser during opening WebView, set webviewclient to display it in WebView.

And rewrites the onbackpressed () method. When the WebView is not empty and the WebView can fallback, it returns to the previous WebView interface instead of directly fallback to the previous activity.

Because the application needs to access the Internet, it needs to be in Android manifest Configuration in XML:

The effect diagram of this example is as follows:

II WebView loads HTML code

WebView provides a loaddata (string data, string mimeType, string encoding) method, which can be used to load and display HTML code, but it will cause garbled code.

WebView also provides a loaddatawithbaseurl (string baseurl, string data, string encoding, string historyurl) method, which is an enhanced version of the loaddata (string data, string encoding) method and will not generate garbled code. Description of several parameters of the method:

Data: Specifies the HTML code to load.

MimeType: Specifies the MIME type of HTML. For HTML, text / HTML can be specified.

Encoding: Specifies the character set used for HTML code encoding, such as GBK.

WebView load HTML code:

The running effect of this example is as follows:

III JavaScript in WebView calls Android methods

There are three steps to invoke the Android method in WebView:

Call setjavascript enabled (true) of websettings associated with WebView to enable JavaScript calling function.

Call the addjavascript interface (object, string name) method of WebView to expose the object object to the JavaScript object.

Call the Android method through the name object just exposed in the JavaScript script.

Write an example of calling the Android method in JavaScript, which contains a WebView component to display HTML pages. The activity code is as follows:

In the above code, the function of JavaScript calling Android methods is enabled, and the myobj object in the Android application is exposed to the JavaScript script as an object named myobj in the JavaScript script.

The MyObject code is as follows:

MyObject contains two methods, showtoast () and showlist (). These two methods are decorated with @ javascriptinterface, so that these two methods will be exposed to JavaScript scripts, allowing JavaScript scripts to call these two methods through myobj. The HTML page code is as follows:

When the user clicks two buttons on the page, the JavaScript script of the page will call the Android method through myobj.

Run the instance and click the first button. The effect diagram is as follows:

Click the second button, and the effect diagram is as follows:

The above is the Android WebView application interface development tutorial introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>