Android 单击第二个链接后在活动中嵌入网页

Android 单击第二个链接后在活动中嵌入网页,android,webview,android-activity,android-webview,Android,Webview,Android Activity,Android Webview,我在活动中打开了一些网页,当我从应用程序中单击链接时,它会打开自身的链接。但当页面加载时,若我单击网页中的某个链接,它将在系统的默认浏览器中打开。我怎样才能预防它 例如: 如果我从应用程序中单击,它将在应用程序中打开 然后nytimes加载,我点击nytimes菜单中的,现在在Chrome浏览器中打开 这是我在onCreate中的代码部分 您应该将WebViewClient添加到WebView中,然后重写shouldOverrideUrlLoading方法,如下所示 myWebView.setW

我在活动中打开了一些网页,当我从应用程序中单击链接时,它会打开自身的链接。但当页面加载时,若我单击网页中的某个链接,它将在系统的默认浏览器中打开。我怎样才能预防它

例如:

如果我从应用程序中单击,它将在应用程序中打开

然后nytimes加载,我点击nytimes菜单中的,现在在Chrome浏览器中打开

这是我在onCreate中的代码部分


您应该将WebViewClient添加到WebView中,然后重写shouldOverrideUrlLoading方法,如下所示

myWebView.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // do what you want

        // return true if the host application wants to leave the current 
        // WebView and handle the url itself, otherwise return false.
        return true;
    }
});
myWebView.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // do what you want

        // return true if the host application wants to leave the current 
        // WebView and handle the url itself, otherwise return false.
        return true;
    }
});