When opening a web page, the system browser will not be called, but will be displayed in this WebView:
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
Calling JavaScript through Java code
WebSettings webSettings = mWebView .getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new Object() {
public void clickOnAndroid() {
mHandler.post(new Runnable() {
public void run() {
webview.loadUrl("javascript:wave()");
}
});
}
}, "demo");
When you press the back key, you do not exit the program but return to the previous browsing page:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView .canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
When opening a page, the adaptive screen:
WebSettings webSettings = mWebView .getSettings();
webSettings.setUseWideViewPort(true);//Set this property to scale at any scale
webSettings.setLoadWithOverviewMode(true);
Page support zoom:
WebSettings webSettings = mWebView .getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
If the user is required to enter the user name, password or other information manually in WebView, WebView must set support for getting gesture focus.
webview.requestFocusFromTouch();
The WebView loading interface mainly calls three methods: LoadUrl, LoadData, LoadDataWithBaseURL
- LoadUrl loads web pages, pictures and displays them directly
- LoadData displays text and picture content (simulator 1.5, 1.6)
- LoadDataWithBase displays text and picture content (supports multiple simulator versions)
Common methods of WebSettings
setJavaScriptEnabled(true); //Support js
setPluginsEnabled(true); //Support plugins
setUseWideViewPort(false); //Resize the picture to fit webview
setSupportZoom(true); //Support zoom
setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); //Support content re layout
supportMultipleWindows(); //Multiple windows
setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //Turn off caching in webview
setAllowFileAccess(true); //Set access to files
setNeedInitialFocus(true); //Set the node for webview when it calls requestFocus
webview webSettings.setBuiltInZoomControls(true); //Set support zoom
setJavaScriptCanOpenWindowsAutomatically(true); //Support to open new windows through JS
setLoadWithOverviewMode(true); // Zoom to screen size
setLoadsImagesAutomatically(true); //Support for automatically loading pictures
A complete solution to WebView client
doUpdateVisitedHistory(WebView view, String url, boolean isReload) //(update history)
onFormResubmission(WebView view, Message dontResend, Message resend) //(application re requests web data)
onLoadResource(WebView view, String url) // It will be called when loading page resources, and every resource (such as picture) will be called once.
onPageStarted(WebView view, String url, Bitmap favicon) //This event is called from the loading page. Usually we can set a loading page here to tell the user that the program is waiting for the network response.
onPageFinished(WebView view, String url) //Called at the end of the page load. In the same way, we know that a page is loaded, so we can close the loading bar and switch the program action.
onReceivedError(WebView view, int errorCode, String description, String failingUrl)// (report error messages)
onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host,String realm)//(get return information authorization request)
onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) //Overriding this method allows webview to process https requests.
onScaleChanged(WebView view, float oldScale, float newScale) // (called when WebView changes)
onUnhandledKeyEvent(WebView view, KeyEvent event) //(called when the Key event is not loaded)
shouldOverrideKeyEvent(WebView view, KeyEvent event)//Override this method to handle key events in the browser.
shouldOverrideUrlLoading(WebView view, String url)
//If the link requested by clicking is called, rewrite this method to return true to indicate whether clicking the link in the web page or jumping in the current webview without jumping to the browser. We can do many operations for this function. For example, we can read some special URL s, so we can cancel this operation without opening the address, and perform other predefined operations, which is very necessary for a program
Reprinted from: http://blog.csdn.net/ljx19900116/article/details/45028513