The WebView component of Android can be said to be quite powerful. Now summarize the several functions that are often used in the project as follows:
1. Background settings
(0);//Set the background color to transparent first();//Then set the background image
2. Obtain the initialization and completion events of WebView webpage loading
step:
1 Create your own WebViewClient (inheriting the WebViewClient class) such as WebViewClient
2 Overload the onPageFinished (WebView view, String url) method inside (the method will be called after the webview is loaded). This method puts what you want to do and after the webview is loaded
3 Associate your own webviewclient and webview through this method:( new WebViewClient();
(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { //Finish(view, url); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { //start (view, url, favicon); } });
If you need to monitor the loading progress, you need to create your own WebChromeClient class and overload the method onProgressChanged, and then
(new MyWebChromeClient())Just: class MyWebChromeClient extends WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub (view, newProgress); } } For example: public class WebPageLoader extends Activity { final Activity activity = this; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); ().requestFeature(Window.FEATURE_PROGRESS); setContentView(); WebView webView = (WebView) findViewById(); ().setJavaScriptEnabled(true); ().setSupportZoom(true); (new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { ("Loading..."); (progress * 100); if (progress == 100) (.app_name); } }); (new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Handle the error } public boolean shouldOverrideUrlLoading(WebView view, String url) { (url); return true; } }); (""); } }
Layout file:
<?xml version="1.0" encoding="utf-8"?> <WebView xmlns:andro android: android:layout_width="fill_parent" android:layout_height="fill_parent" />
It should be noted that the webView has a series of usages, such as ().setJavaScriptEnabled(true); can be set using javscript;
().setJavaScriptEnabled(true); (WebView.SCROLLBARS_OUTSIDE_OVERLAY); (false); ().setSupportZoom(true); ().setBuiltInZoomControls(true); (70); (true);
So for details, please refer to the API
The use of the progress bar is to write the onProgressChanged event in the internal class after a setWebChromeClient is released in new.
Summarize:
In the design of WebView, not everything is done by the WebView class. Some errands are distributed to others. In this way, WebView can concentrate on its own parsing and rendering work. WebViewClient helps WebView handle various notifications and request events, specifically including:
- onLoadResource
- onPageStart
- onPageFinish
- onReceiveError
- onReceivedHttpAuthRequest
WebChromeClient is a dialog box, website icon, website title, loading progress, etc. that assists WebView to handle Javascript.
- onCloseWindow (Close WebView)
- onCreateWindow()
- onJsAlert (There is no thing to pop up on the alert on the WebView, and you need to customize your WebChromeClient to handle popups)
- onJsPrompt
- onJsConfirm
- onProgressChanged
- onReceivedIcon
- onReceivedTitle
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.