2016-01-25 73 views
0

我遇到了一種我認爲是常見情況的情況,但我很難找出解決方案。 This是我發現類似於我的問題,但我不明白如何解決方案爲他工作。任何人都可以用正確的方式指出我的意思,或者說明答案是否正確。根據用戶選擇覆蓋webview url

我只是想,如果用戶說yes否則沒有壓倒一切,這是我的一段代碼

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

      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      final boolean result[] = new boolean[1]; 
      builder.setTitle("Confirm"); 
      builder.setMessage("Are you sure to Finish Process?"); 

      builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
        //some code 

        result[0] = true; 
        dialog.dismiss(); 
       } 

      }); 

      builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // some code 
        result[0] = false; 
        dialog.dismiss(); 
       } 
      }); 

      AlertDialog alert = builder.create(); 
      alert.show(); 
      return result[0]; 

}覆蓋的WebView網址

我需要的WebView等待用戶的響應,這是現在不會發生,最初爲假的布爾型result[0]立即返回。

如果我缺少任何東西,請點我。

回答

0

AlertDialog不會阻塞UI線程。
顯示AlertDialog時,它不會停止WebView繼續使用新的url更新UI。

您需要首先防止加載url,然後在用戶做出決定後再次致電webview.loadUrl

private WebView mWebView; 
private boolean mUserHasAllowedUrlLoading = false; 
private String mUrl; 
... 

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

    // Store the url 
    mUrl = url; 

    // Keep the WebView so we can access it again in the AlertDialog's anonymous inner classes 
    mWebView = view; 

    if (!mUserHasAllowedUrlLoading) { 
     // This is the first time the url has attempted to load, so show the dialog. 

     // Build and show AlertDialog 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Confirm"); 
     builder.setMessage("Are you sure to Finish Process?"); 

     builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 

       mUserHasAllowedUrlLoading = true; 

       // Start loading the stored url again 
       mWebView.loadUrl(mUrl); 

       dialog.dismiss(); 
      } 

     }); 

     builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 

       dialog.dismiss(); 
      } 
     }); 

     AlertDialog alert = builder.create(); 
     alert.show(); 

     // We return true here so the WebView does not load anything for the moment 
     return true; 

    } else { 
     // We can only get here if the user has already seen the dialog and clicked the positive button. 

     // Reset the boolean so it's ready for next time the use tries to navigate away from the current page. 
     mUserHasAllowedUrlLoading = false; 

     // We return false this time so that the WebView actually loads the supplied url. 
     return false; 
    } 
}