How to click a link in WebView to open it by yourself

Source: Internet
Author: User

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.