【问题标题】:Don't navigate to other pages in WebView,disable links and references不要导航到 WebView 中的其他页面,禁用链接和引用
【发布时间】:2013-10-25 22:39:52
【问题描述】:

我在 Android 中有一个 webView,我在其中打开了一个 html 网页。但它充满了链接和图像,当我单击其中一个时,它会加载到我的 webview 中。我想禁用这种行为,所以如果我点击一个链接,不要加载它。我试过这个solution 并为自己编辑了一点,但没有奏效。 我的 webviewclient 代码:

private boolean loaded = false;

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

    if(loaded == false){
        view.loadUrl(url);
        loaded = true;
        return true;
    }else{
        return false;
    }

}

我的 webview 实现和设置。

WebView wv = (WebView) findViewById(R.id.recipeWv);
    ourWebViewClient webViewClient = new ourWebViewClient();
    webViewClient.shouldOverrideUrlLoading(wv, URLsave);
    wv.setWebViewClient(webViewClient);
    wv.setFocusableInTouchMode(false);
    wv.setFocusable(false);
    wv.setClickable(false);
    WebSettings settings = wv.getSettings();
    settings.setDefaultTextEncodingName("utf-8");

    settings.setLoadWithOverviewMode(true);
    settings.setBuiltInZoomControls(true);

示例:如果用户在我的网页视图中打开 StackOverflow 主页并单击其中一个链接(如“问题”),则网页视图应保留在 StackOverflow 主页上。

【问题讨论】:

    标签: android html webview


    【解决方案1】:

    您应该将您当前的 url 保存在一个类变量中,并检查它是否与加载的相同。当用户点击链接时,shouldOverrideUrlLoading 被调用并检查网站。 像这样的:

    private String currentUrl;
    
    public ourWebViewClient(String currentUrl) {
        this.currentUrl = currentUrl;
    }
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.equals(currentUrl)) {
            view.loadUrl(url);  
        }
        return true;
    }
    

    重要提示:不要忘记将WebViewClient 设置为您的WebView

    ourWebViewClient webViewClient = new ourWebViewClient(urlToLoad);
    wv.setWebViewClient(webViewClient);
    

    【讨论】:

    • 我们能以某种方式禁用点击吗?菜单在 webview 中仍然可以展开,它显示了导航链接,但在此解决方案之后当然无法导航。
    【解决方案2】:

    实现一个 WebViewClient 并从 WebView Client 的这个方法返回 true

    webView.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }
    });
    

    【讨论】:

    • 停止/防止 webview 打开内容中包含的任何 url 的最正确方法。 **** --- 只是为了停下来-- ****
    【解决方案3】:

    如果你想在不同的窗口中打开网页的内部链接,那么

    不要使用

    WebView webView;//Your WebView Object
    webView.setWebViewClient(new HelpClient());// Comment this line
    

    B'coz setWebViewClient() 方法负责在同一个 webview 中打开一个新页面。这么简单的评论这一行。

    private class HelpClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.example.com")) {
                // This is my web site, so do not override; let my WebView load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
    

    希望这会奏效。

    【讨论】:

    • 我不想导航到其他页面,保持不变。所以就像禁用引用和链接。
    • @Dandelion 您正在从资产加载静态 html 页面还是 URL?
    • 这是一个 URL。我在 ListView 中有很多引用,当用户单击其中一个时,它会在 webview 中打开。
    【解决方案4】:

    即使我也面临同样的问题。我解决了这个问题,如下所示

    webView.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            return true;
                        }
                    });
    

    使用 lambda 表达式

    webView.setOnTouchListener((v, event) -> true);
    

    【讨论】:

    • 这会阻止用户滚动,因此它仅在您不需要在 WebView 内滚动时才有效
    【解决方案5】:

    请注意,API 24 中的方法签名发生了变化。

    对于早于 24 的 API,第二个参数是 String,现在不推荐使用该签名。

    对于 API 24 及更高版本,第二个参数是 WebResourceRequest。

    如果您的应用同时支持 pre 和 post API 24,并且您想要禁用所有链接,您可以使用此:

      webView.setWebViewClient(new WebViewClient(){
            @Override //for APIs 24 and later
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){ 
               return true; 
            }
            @Override //for APIs earlier than 24
            public boolean shouldOverrideUrlLoading(WebView view, String url){
                return true;
            }
        });
    

    【讨论】:

      【解决方案6】:

      你需要多加一行代码,像这样-

      webview.loadUrl(url);
      webview.setWebViewClient(new MyWebViewClient());
      

      并像这样添加一个额外的类 MyWebViewClient-

      /* Class for webview client */
      class MyWebViewClient extends WebViewClient {
      
      // show the web page in webview but not in web browser
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
      
          view.loadUrl(url);
      
          return true;
      }
      
      @Override
      public void onPageFinished(WebView view, String url) {
          super.onPageFinished(view, url);
      }
      
      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
          super.onPageStarted(view, url, favicon);
      }
      
      @Override
      public void onLoadResource(WebView view, String url) {
          super.onLoadResource(view, url);
      
      }
      
      }
      

      【讨论】:

      • 是的,我使用它,我将通过实施更新我的问题。
      • 我已经更新了我的答案。这似乎对我很有效。希望对您有所帮助。
      【解决方案7】:

      与 Amit Gupta 的回答非常相似,但我找到了更短的方法。

      private String currentUrl;
      
      public CustomWebViewClient(String currentUrl) {
          this.currentUrl = currentUrl;
      }
      
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          return ! url.equals(currentUrl);
      }
      

      这个CustomWebViewClient定义好后,设置客户端为webview。

      CustomWebViewClient webViewClient = new CustomWebViewClient(urlToLoad);
      wv.setWebViewClient(webViewClient);
      

      【讨论】:

        【解决方案8】:

        @Override onPageFinished

        上使用注入 javascript
        view.loadUrl(
            javascript: (function () {
        
        
                document.addEventListener('click', function (e) {
                    e.stopPropagation();
                }
                    , true);
        
            })()
        );
        

        【讨论】:

        • edit您的答案,以确保它改进了此问题中已经存在的其他答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-21
        • 1970-01-01
        • 2014-01-03
        • 2020-07-31
        • 2013-05-11
        相关资源
        最近更新 更多