问题
I am currently writing a webview, it first loads a twitter page (say, NHL, http://twitter.com/nhl) as you can see, you can find the tweet for NHL, and each NHL tweet has another link for user to click, e.g. bit.ly/ujcNZo
in my webview, if i click that link (i.e. bit.ly/ujcNZo), my webview, after 1 second, doesn't display anything but a twitter icon on a white color background, it should load the content but it didn't.
after some time of investigation, i think it has to do with the fact that the link in the tweet (i.e. bit.ly/ujcNZo) actually opens the link in a separate window (pop up?) and not the current page where the link is posted, i verified this by going to NHL's twitter page on a browser in my laptop.
My Question is, 1. is there a way i can load the content of the external link (bit.ly/ ujcNZo, for instance) in my current webview?
回答1:
You can control this through the WebViewClient class. Simply extend it and override the default implementation to get the desired configuration then set it as the client for your current webview.
Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the webViewClient to a new instance of your custom WebViewClient
webview.setWebViewClient(new WebActivityClient( this ));
}
Custom Client
/** WebViewClient class for WebView in WebActivity */
private class WebActivityClient extends WebViewClient {
public static final String TAG = "WebActivityClient";
private Context context;
/** Constructor used to grab the context */
public WebActivityClient( Context context ) {
this.context = context;
}
/** Override to load every link within the page inside this webview instead of using
* android's default application
* #7 http://developer.android.com/resources/tutorials/views/hello-webview.html */
@Override
public boolean shouldOverrideUrlLoading( WebView view, String url ) {
view.loadUrl(url);
return true;
}
}
Hope this helps!
来源:https://stackoverflow.com/questions/8647383/open-pop-up-external-site-link-in-current-webview