android 中的 webView 在 web 而不是 webview 中打开网站

webView in android open the website in web instead of webview

当我 运行 此代码时,"google.com" 在 Web 应用程序(例如 google chrome)中打开,而不是在对话框的 webView 中打开。 它确实适用于某些网址。 为什么?

    final Dialog dialog=new Dialog(this.activity,android.R.style.Theme_Black_NoTitleBar_Fullscreen);

    dialog.setContentView(R.layout.full_page_ad_between_chapters);
    WebView webView = (WebView) dialog.findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://google.com");

R.layout.full_page_ad_between_chapters :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="350dp"
    android:layout_height="1000dp"
    android:layout_margin="5dp"
    android:orientation="vertical" >


    <Button
        android:id="@+id/fullPageAdBetweenChaptersClose"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:text="סגור" />

        <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

您需要覆盖方法 shouldOverrideUrlLoading() on your WebViewClient

WebView webView = (WebView) dialog.findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // check if we want to open the url into the WebView
        if (iWantToOpenTheUrlIntoTheWebView(url)) {
            return false;
        }
        // let the system handle the url outside of the app
        return true;
    }
});