Objective
WebView (Network view) is used to display Web pages in Andorid, let's take a look at how it is used.
First, the basic use
1. Declaring permissions, WebView inevitably to use the network, we want to add network access rights.
<uses-permission android:name= "Android.permission.INTERNET"/>
2. Put in Layout
<webview android:layout_width= "match_parent" android:layout_height= match_parent "android:id=" @+id/
Web ">
3. References in activity
Private WebView WebView;
WebView = (webview) Findviewbyid (r.id.web);
Webview.loadurl ("http://piaoshu.org");
Webview.loadurl ("file:///android_asset/xx.html"), or you can load local HTML
You'll find that apps automatically jump out of the system or third-party browsers, which means you can't display your own pages in your application. What do we do? Next, we introduce the WebView two important listening events.
Ii. Webviewclient and Webchromeclient
webView.setWebViewClient()
Mainly used to load the Web page process of listening.
Webview.setwebviewclient (New Webviewclient () {
@Override
///As long as you override this method, you can load the Web page public boolean in this application
Shouldoverrideurlloading (webview view, String URL) {
view.loadurl (URL);
Return true;//returns True when the control page is opened in WebView, if the system browser or third party browser is invoked for false
@Override public
void Onreceivederror (webview view, int errorcode,
string description, String failingurl) {
//When an error message is received, the system performs this action. c14/>//For example, when there are 404 error codes, we can write HTML ourselves in the asset folder, hide the WebView and display the local page.
}
@Override public
void onpagestarted (webview view, String URL, Bitmap favicon) {
super.onpagstarted (view, URL, Favicon);
If you want to do something when the page starts to load, override the method
}
@Override public
void onpagefinished (webview view, String URL) {
super.onpagefinished (view, URL);
Override the method} if you want to do something at the end of the page
;
webView.setWebChromeClient()
Primarily used to load Web page procedures for UI operations.
Webview.setwebchromeclient (New Webchromeclient () {
@Override public
void onprogresschanged (webview view, int newprogress) {
progressbar.setprogress (newprogress);
Newprogress values are 1 to 100 integers, we can use this parameter to display the load progress ProgressBar or ProgressDialog
}
} @Override
public
Void Onreceivedtitle (WebView view, String title) {
Textview.settext (title);//argument title is the title of the Web page and can be displayed with a textView.
}
}
);
OK, let's take a look at the loading of the page.
It looks like the page is loading well, but I've already set some properties. Let's see what attributes WebView have.
Third, websettings
WebSettings settings=webview.getsettings ();
Supports JavaScript script
settings.setjavascriptenabled (TRUE);
Set WebView supports a wide range of Windows
Settings.setusewideviewport (TRUE);
Support gesture Scaling
settings.setbuiltinzoomcontrols (true);
Set WebView supports loading more format pages
Settings.setloadwithoverviewmode (TRUE);
WebView Load page takes precedence over cache loading
settings.setcachemode (websettings.load_cache_else_network);
There are some attributes I will not speak out, interested in the search for their own.
Four, download the file
WebView sometimes contains links to downloaded files, and the files are downloaded to the local area after clicking the link. How to achieve it? The system provides us with a download interface Downloadlistener, the code is simple and convenient.
We simply write a class that implements this interface and overrides the method, invoking the system's browser via intent
Class Mylistenter implements downloadlistener{
@Override public
void Ondownloadstart (string url, string useragent, string contentdisposition, String mimetype, long contentlength) {
uri uri = uri.parse (URL); URL is the download link
Intent Intent = new Intent (Intent.action_view, URI);
StartActivity (intent);
}
Don't forget to set up the listener
Webview.setdownloadlistener (New Mylistenter ());
We use a third party browser to download, this is the simplest way
Click on the Android download, the results are as follows
Or we can write a thread to download, start the thread in the onDownloadStart()
method can be, the specific code is not posted.
Summarize the usage.
Remember to declare permission before use, and the reference to the control is needless to say.
The focus is on Webviewclient and webchromeclient two classes.
webView.setWebViewClient()
Mainly used to load the Web page process related operations.
webView.setWebChromeClient()
is used to load the action of the Web page procedure on the UI.
The system also provides us with a download interface Downloadlistener.
The above is the entire content of this article, I hope that the study or work to bring some help, if you have questions you can message exchange.