Android WebView - Intercept clicks

前端 未结 1 1969
旧巷少年郎
旧巷少年郎 2020-11-27 15:14

I have written a simple helloworld app with a WebView which has a link to CNN on a simple.html page in my asset folder.

cnn.         


        
相关标签:
1条回答
  • 2020-11-27 15:51

    Then you have to set a WebViewClient to your WebView and override shouldOverrideUrlLoading and onLoadResource methods. Let me give you a simple example:

    WebView yourWebView; // initialize it as always...
    // this is the funny part:
    yourWebView.setWebViewClient(yourWebClient);
    
    // somewhere on your code...
    WebViewClient yourWebClient = new WebViewClient(){
        // you tell the webclient you want to catch when a url is about to load
        @Override
        public boolean shouldOverrideUrlLoading(WebView  view, String  url){
            return true;
        }
        // here you execute an action when the URL you want is about to load
        @Override
        public void onLoadResource(WebView  view, String  url){
            if( url.equals("http://cnn.com") ){
                // do whatever you want
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题