When I recently wrote an application, I used WebView, but suddenly encountered a problem. The webpage that was first loaded is displayed in the Custom webView. However, when I click the link on the webpage, prompt to use another browser in the system. This is not good,
How can I display the webpage after clicking the link? So I checked the Android document on Google and found a method. The following is a source code explanation.
Package demo. androidyue. webview;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. webkit. WebView;
Import android. webkit. WebViewClient;
Public class WebViewDemoActivity extends Activity {
Private WebView webView;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Initialize WebView
This. initWebView ();
}
/*
* Initialize WebView
*/
Private void initWebView (){
// Expand webView from the layout File
This. webView = (WebView) this. findViewById (R. id. webview );
// Set WebViewClient for WebView to process some operations
This. webView. setWebViewClient (new webViewClient ());
// Load the address
This. webView. loadUrl ("http: // 10.0.2.2 ");
}
Class webViewClient extends WebViewClient {
// Rewrite the shouldOverrideUrlLoading method so that you can click the link without using other browsers.
@ Override
Public boolean shouldOverrideUrlLoading (WebView view, String url ){
View. loadUrl (url );
// If you do not need to process other Click link events, return true; otherwise, return false.
Return true;
}
}
}
The key is to set WebViewClient for WebView, and then rewrite the shouldOverrideUrlLoading method. Among them, WebViewClient is a helper class of WebView, which mainly handles various notifications and request events.
Last Modified: 2011-12-06 http://thinkblog.sinaapp.com
From BossDarcy's column