Summary of simple use of WebView in Android Development

preface

WebView is used to display web pages in andorid. Let's take a look at how it is used.

1、 Basic use

1. Declare permissions. WebView inevitably uses the network. We need to add network access permissions.

2. Put in layout

3. Reference in activity

At this time, you will find that the application will automatically jump out of the system or third-party browser, that is, you can't display your web page in your own application. What shall I do? Next, we introduce two important listening events of WebView.

2、 Webviewclient and webchromeclient

webView. Setwebviewclient() is mainly used to listen to the process of loading web pages.

webView. Setwebchromeclient() is mainly used for UI operations during web page loading.

Well, let's take a look at the web page loading.

It seems that the web page loads well. In fact, I have set some related properties. Let's see what properties WebView has

3、 Websettings

I won't tell you about some other attributes. If you are interested, search for them yourself.

4、 Download File

WebView sometimes contains a link to download files. After clicking the link, the files will be downloaded locally. How to achieve it? The system provides us with a download interface downloadlistener. The code is simple and convenient.

We just need to write a class to implement this interface and rewrite the method to call the system browser through intent

Don't forget to set up a listener

We download with the help of a third-party browser, which is the simplest way

Click Android download, the effect is as follows

Or we can write a thread to download. Just start the thread in the ondownloadstart () method, and the specific code will not be posted.

Summarize the usage.

Remember to declare the permission before use, and the reference of the control goes without saying.

The focus is on webviewclient and webchromeclient.

webView. Setwebviewclient() is mainly used to load web pages.

webView. Setwebchromeclient () is used to load the UI operation of the web page process.

The system also provides us with a download interface downloadlistener.

The above is the whole content of this article. I hope it will bring some help to your study or work. If you have any questions, you can leave a message.

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