1. When opening a webpage, the system browser is not called, but displayed in this WebView:
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
2. Call JavaScript from 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");
3. When pressing the back key, it does not exit the program but returns 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);
}
4. When opening the page, adaptive screen:
WebSettings webSettings = mWebView .getSettings();
webSettings.setUseWideViewPort(true);//Set this property, it can be scaled arbitrarily
webSettings.setLoadWithOverviewMode(true);
5. The page supports zooming:
WebSettings webSettings = mWebView .getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
6. If the webView requires the user to manually enter the username, password or other, the webview must be set to support getting the gesture focus.
webview.requestFocusFromTouch();
7. The WebView loading interface mainly calls three methods: LoadUrl, LoadData, LoadDataWithBaseURL.
1. LoadUrl directly loads web pages, pictures and displays them. (Web pages, pictures, gifs locally or on the Internet)
2. LoadData displays text and picture content (simulator 1.5, 1.6)
3. LoadDataWithBase displays text and image content (supports multiple simulator versions)
8. Introduction to common methods of WebSettings
setJavaScriptEnabled(true); //支持js
setPluginsEnabled(true); //Support plugins
setUseWideViewPort(false); //Adjust the picture to fit the size of the webview
setSupportZoom(true); //support zoom
setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); //Support content re-layout
supportMultipleWindows(); //Multiple windows
setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //Close the cache in webview
setAllowFileAccess(true); //Set the file that can be accessed
setNeedInitialFocus(true); //Set the node for webview when webview calls requestFocus
webview webSettings.setBuiltInZoomControls(true); //Set to support zoom
setJavaScriptCanOpenWindowsAutomatically(true); //Support opening new windows through JS
setLoadWithOverviewMode(true); // zoom to the size of the screen
setLoadsImagesAutomatically(true); //Support automatic loading of images
9. Full solution to the methods of WebViewClient
doUpdateVisitedHistory(WebView view, String url, boolean isReload) //(Update history)
onFormResubmission(WebView view, Message dontResend, Message resend) //(The application re-requests web page data)
onLoadResource(WebView view, String url) // Called when the page resource is loaded, and each resource (such as an image) is loaded once.
onPageStarted(WebView view, String url, Bitmap favicon) //This event is called to start loading the 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 page loading. In the same way, we know that a page is loaded, so we can close the loading bar and switch program actions.
onReceivedError(WebView view, int errorCode, String description, String failingUrl)// (report error message)
onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host,String realm)//(Get the return information authorization request)
onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) //Override this method to allow webview to handle https requests.
onScaleChanged(WebView view, float oldScale, float newScale) // (Called when the 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)
//It will only be called when the click request is the link. Overriding this method to return true indicates that clicking the link in the webpage still jumps in the current webview, not to the browser. We can do a lot of operations with this function, for example, we read some special URL, so we can cancel this operation without opening the address, and perform other predefined operations, which is very necessary for a program.