2017-08-28 20 views
0

在我的應用程序,有四個選項卡,並在第二個選項卡中,我試圖加載webview ... 後禁用shouldOverrideUrlLoading它完美加載片段中的webview。 .. 但shouldOverrideUrlLoading方法,它重定向對新的活動...... 但我要的是加載網頁流量第一的片段,點擊任何內容鏈接應該在新的活動被重定向後如何限制調用shouldOverrideUrlLoading當第一次webview加載片段

private void setUpWebView() { 

    WebSettings ws = webView.getSettings(); 
    ws.setJavaScriptEnabled(true); 
    ws.setAllowFileAccess(true); 
    webView.setScrollContainer(false); 
    webView.setWebViewClient(new WebViewClient()); 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    mContext = context; 
} 

private class WebViewClient extends android.webkit.WebViewClient{ 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) 
    { 
     Intent intent = new Intent(getContext(),WebViewShowActivity.class); 
     startActivity(intent); 
     return true; 
    } 

} 
+0

你是什麼意思?有'shouldOverrideUrlLoading'方法不會加載你的'webview'嗎? – Wizard

+0

是的,它確實....當我禁用此方法,並在片段中寫入此方法後,它在另一個activity.But中重定向,但我想加載片段中的webview,但此方法重定向到另一個活動,...但webview應該是負載在片段和點擊任何鏈接,它應該重定向到其他活動 – Nimesh

回答

0
public class WebViewJavaScriptInterface { 
     public Context context; 

     public WebViewJavaScriptInterface(Context context) { 
      this.context = context; 
     } 

     @JavascriptInterface 
     public void openPost(String url,String posttitle) { 
      String postUrl = "https://google.in"+url; 
      String postTitle = posttitle; 
      Log.d("POSTURL",postUrl); 
      Intent intent = new Intent(getContext(), WebViewShowActivity.class); 
      intent.putExtra("linkUrl",postUrl); 
      intent.putExtra("postTitle",postTitle); 
      startActivity(intent); 
     } 

     @JavascriptInterface 
     public void openVideo(String url) { 
      String youtubeUrl = url; 
      Log.d("YoutubeURL",youtubeUrl); 
      ForTrainertUtil.playPostVideoForYoutubeID(context,youtubeUrl); 
     } 


    } 

這是我們如何能夠處理通過JavaScript .. 點擊來自網頁的事件,當我在網頁的具體內容點擊處理android系統關聯方法。

0

你可以檢查條件

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) 
{ 
    if (view.getUrl()!=baseUrl) { 
     Intent intent = new Intent(getContext(), WebViewShowActivity.class); 
     startActivity(intent); 
     return true; 
    }else { 
     return false; 
    } 
} 
+0

是的,你是對的,但我做了這diffrently ..看到我發佈了我的答案。 – Nimesh

0

要在webview中加載url,您應該將其加載到webview對象中,該對象在「view」回調中返回,並打開webview內容中的點擊鏈接,您應該檢查請求並加載新活動。

webview.setWebViewClient(new WebViewClient() { 
      public boolean shouldOverrideUrlLoading(WebView view, String request) { 
       if (request != null && request.toString().startsWith("http://")) { //comparison params is upto your requirements 
        Intent intent = new Intent(MainActivity.this, secondactivity.class); 
        startActivity(intent); 
        return true; 
       } 
       return super.shouldOverrideUrlLoading(view, request); 
      } 
     }); 
+0

是的,你是對的,但我做了這個diffrently ..見我已經張貼我的答案。 – Nimesh

相關問題