Android WebView 在默认浏览器中打开链接 - 在应用程序中打开链接

Android WebView opening links in default browser - Open links in app

我用 WebView 属性 创建了一个 android 应用程序,一切都很好,但唯一的问题是当我在该 WebView 中单击任何 link 时,它会自动进入默认状态网页浏览器。但我只想在我的应用程序 Web 视图中打开 links。这是我的代码:

mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://plan.regenbogen-gesamtschule.de/vertretung/schueler/subst_001.htm");
    mWebView.setWebViewClient(new MyAppWebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            //hide loading image
            findViewById(R.id.progressBar1).setVisibility(View.GONE);
            //show webview
            findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
        }});

如果您在 MyAppWebViewClient 和 return false 中覆盖 shouldOverrideUrlLoading(...),您的网络视图应该会自动尝试加载 url。文档说明如下:

Return: True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.

请参阅 shouldOverrideUrlLoading(...) 文档。

 mWebView.setWebViewClient(new MyAppWebViewClient(){

    @Override
    boolean shouldOverrideUrlLoading (WebView view, String url) {
        return false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        //hide loading image
        findViewById(R.id.progressBar1).setVisibility(View.GONE);
        //show webview
        findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
    }});