Android WebView click open within WebView not a default browser

后端 未结 12 1931
迷失自我
迷失自我 2020-11-27 16:34

I did one sample application using WebView, in that web view the URL comes from web services. It\'s working fine, but if I click any link within that WebView, its automatica

相关标签:
12条回答
  • 2020-11-27 17:10

    This Will open any particular link only in your app ...

    WebView web;
    
    web = (WebView) findViewById(R.id.web);
    web.setWebViewClient(new WebViewClient(){
    
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null && url.startsWith("http://www.xplorerstrick.net")) {
                view.loadUrl(url);
                progDailog.show();
                return true;
            }
            else {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 17:20

    You need to call wvBikeSite.setWebViewClient, e.g:

        MyWebViewClient wvc = new MyWebViewClient();
        wvBikeSite.setWebViewClient(wvc);
    

    Where MyWebViewClient overrides shouldOverrideUrlLoading, viz:

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 17:20

    Use this code:

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());       
    
    0 讨论(0)
  • 2020-11-27 17:22

    You have to set up a webViewClient for your webView.

    Sample:

    this.mWebView.setWebViewClient(new WebViewClient(){
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){
          view.loadUrl(url);
          return true;
        }
    });
    
    0 讨论(0)
  • 2020-11-27 17:22

    You can always open url within webview by using this:

          @Override
      public void onCreate(Bundle savedInstanceState) {
         webview.setWebViewClient(new MyWebViewClient());
     webview.getSettings().setJavaScriptEnabled(true);
     webview.getSettings().setPluginsEnabled(true);
         }
    
    
         private class MyWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
    
                view.loadUrl(url);
                return true;
            }
    
            @Override
            public void onLoadResource(WebView  view, String  url){
    
            }
        }    
    
    0 讨论(0)
  • 2020-11-27 17:25

    Try this one, that method is deprecated.

    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
    
    0 讨论(0)
提交回复
热议问题