Detailed explanations of WebView in Android:
1. Concept:
WebView (network view) can load a display page, which can be viewed as a browser. It uses the WebKit rendering engine to load the Display Web page.
2. How to use:
(1). Instantiate the WebView component:
A. Instantiate the WebView component in activity. eg
new WebView(this
B. Call WebView's Loadurl () method to set the page that Wevview want to display. Eg:
互联网用:webView.loadUrl("http://www.google.com");本地文件用:webView.loadUrl("file:///android_asset/XX.html");
C. Call the activity's Setcontentview () method to display the page view.
D. You need to add permissions to the Androidmanifest.xml file, or the Web page not available error will occur.
<uses-permission android:name="android.permission.INTERNET" />
(2). Custom WebView Component Inheritance Webviewclient:
A. Declare the webview in the layout file. eg
<linearlayout xmlns:android =" http://schemas.android.com/apk/res/android " android:layout_width = "match_parent" android:layout_height = "match_parent" > <webview Android:id = "@+id/webview1" android:layout_w Idth = "match_parent" android:layout_height = "match_parent" /> </linearlayout ;
B. Instantiate the WebView in activity.
C. Call WebView's Loadurl () method to set the page WebView to display.
D. Call the Setwebviewclient () method to set the WebView view. Respond to link functionality.
E. You need to add permissions to the Androidmanifest.xml file, or the Web page not available error occurs.
<uses-permission android:name="android.permission.INTERNET"/>
3. The difference between the two methods:
(1). The first method: Click on the link is the new Android system browser in the ring should be linked.
(2). The second method: Click on the link to be handled by yourself, instead of the new Android system browser should link in the ring. Add an Event Listener object (webviewclient) to WebView and override the Shouldoverrideurlloading method in it: responds to Hyperlink buttons in a Web page. When a connection is pressed, Webviewclient calls this method and passes the parameter: the URL that is pressed.
4. Summary:
(1). Use WebView point link to see a lot of pages later in order to let WebView support fallback function, need to overwrite the onkeydown () method covering activity class, if do not do any processing, click the system fallback shear key, the entire browser will call finish () and end itself, Instead of retreating back to the previous page.
@Override publicbooleanonKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { // goBack()表示返回WebView的上一页面 mWebView.goBack(); returntrue; } returnsuper.onKeyDown(keyCode, event); }
(2). Set WebView basic information:
A. If there is JavaScript on the page visited, the WebView must be set to support JavaScript.
webview.getSettings().setJavaScriptEnabled(true);
B. Touch Focus Works:
requestFocus();
C. Cancel the scroll bar:
this
5. The overall code is as follows:
(1). Mainactivity.java
PackageCom.pansoft.webviewdemo;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.KeyEvent;ImportAndroid.view.Window;ImportAndroid.webkit.WebSettings;ImportAndroid.webkit.WebView;ImportCom.pansoft.webviewdemo.webView.MyWebView; Public class mainactivity extends Activity { PrivateWebView Mwebview =NULL;PrivateWebSettings msettings =NULL;/** TAG * / PrivateString TAG = GetClass (). Getsimplename ();/** URL * / PrivateString Flg_url ="http://www.baidu.com/";PrivateMywebview Mywebview;@SuppressLint("Setjavascriptenabled")@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Requestwindowfeature (Window.feature_no_title);//second methodINIT01 ();//The first method //init02 ();}/** * Loaded is the webview itself * / Private void init01() {Setcontentview (r.layout.activity_main); Mwebview = (WebView) Findviewbyid (R.ID.WEBVIEW1); Msettings = Mwebview.getsettings ();//WebView settings support JavaScriptMsettings.setjavascriptenabled (true);//Load URLMwebview.loadurl (Flg_url); Mywebview =NewMywebview ( This, Mwebview); Mwebview.setwebviewclient (Mywebview); }/** * Loaded is the system's own browser * * Private void init02() {Mwebview =NewWebView ( This); Msettings = Mwebview.getsettings (); Msettings.setjavascriptenabled (true); Mwebview.loadurl (Flg_url); Setcontentview (Mwebview); }@Override Public void onbackpressed() {Super. onbackpressed (); }@Override Public Boolean OnKeyDown(intKeyCode, KeyEvent event) {if((keycode = = keyevent.keycode_back) && mwebview.cangoback ()) {//GoBack () indicates that the previous page of the WebView is returnedMwebview.goback ();return true; }return Super. OnKeyDown (KeyCode, event); }}
(2). Mywebview.java
PackageCom.pansoft.webviewdemo.webView;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.webkit.HttpAuthHandler;ImportAndroid.webkit.WebView;ImportAndroid.webkit.WebViewClient;/** * Mywebview * * * @author Administrator * * * Public class mywebview extends webviewclient { PrivateContext Mcontext;PrivateWebView Mwebview;/** * Construction Method * * @param mcontext * @param mwebview * * Public Mywebview(Context Mcontext, WebView Mwebview) {Super(); This. Mcontext = Mcontext; This. Mwebview = Mwebview; }/** * Open the event before the link, in order to avoid the time of loading the system comes with the browser, click the link by yourself * * //This function we can do a lot of things, such as we read to some special URL, so we can not open the address, cancel this operation, for the pre-defined other operations, which is very necessary for a program. @Override Public Boolean shouldoverrideurlloading(WebView view, String URL) {if(URL! =NULL) {mwebview.loadurl (URL); }return true; }/** * receiving an HTTP request event * / @Override Public void onreceivedhttpauthrequest(WebView view, Httpauthhandler handler, string host, String realm) {Super. onreceivedhttpauthrequest (view, Handler, host, realm); }/** * Loading page completed events */ //The same reason, we know that a page loading is complete, so we can close the loading bar, switch program action. @Override Public void onpagefinished(WebView view, String URL) {Super. onpagefinished (view, URL); }/** * Loading page start Event */ //This event is the start of loading the page call, usually we can set a loading in this page, tell the user program is waiting for the network response. @Override Public void onpagestarted(WebView view, String URL, Bitmap favicon) {//TODO auto-generated method stub Super. onpagestarted (view, URL, favicon); }}
(3). Activity_main.xml
< LinearLayout xmlns:android = "HTTP +/ Schemas.android.com/apk/res/android " android:layout_width = "match_parent" android:layout_height =" match_parent "; <webview android:id = "@+id/webview1" android:layout_width =" match_parent " android:layout_height =/> </linearlayout ;
(4). Permissions:
<uses-permission android:name="android.permission.INTERNET" />
Detailed explanations of WebView in Android