在webview中加载url之前的AlertDialog

时间:2014-03-07 09:26:23

标签: android webview

这似乎很容易做到。我只是无法绕过它。 我刚开始使用app开发和java

我正在尝试创建一个确认框“转到此网址?”是/否 - 在网页视图中加载网址时。

到目前为止我得到了什么:

private WebView mWebView;
public ProgressDialog pd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webView);

    // Show loading
    pd = new ProgressDialog(this);
    pd.setMessage("Please wait Loading...");
    pd.show();

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new MyWebViewClient());
    mWebView.loadUrl(getString(R.string.Url));
}

MyWebViewClient - 我需要停止加载并等待用户进行交互:

@Override
public boolean shouldOverrideUrlLoading(final WebView view, String url) {
    Context appContext = view.getContext().getApplicationContext();

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(appContext);
    alertDialog.setTitle("Go to this url?");
    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog,int which)
        {
            // Run the code below on yes - how to I get my parameters in here?
        }
    });

    alertDialog.setNeutralButton("No", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {
            // Cancel the request, but how?
        }
    });

    // Run this code on Yes
    if(Uri.parse(url).getHost().startsWith(String.valueOf(R.string.Url))) {
        return false;
    }
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    view.getContext().startActivity(intent);
    return true;
}

附带问题(仍然相关) - 是否有更好的方法来访问其他类中的参数:

@Override
public void onPageFinished(WebView view, String url) {
    // This is far from pretty, but I need to get Progress Dialog
    MainActivity host = (MainActivity) view.getContext();

    if (host.pd.isShowing()) {
        host.pd.dismiss();
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:2)

我不确定,但尝试这样:

@Override
public boolean shouldOverrideUrlLoading(final WebView view, String url) {
boolean flag=true; 

Context appContext = view.getContext().getApplicationContext();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(appContext);
alertDialog.setTitle("Go to this url?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
    public void onClick(DialogInterface dialog,int which)
   {

    if(Uri.parse(url).getHost().startsWith(String.valueOf(R.string.Url))) {
    flag=false;

     }else{
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
       startActivity(intent);
       flag=true;
      }

    }
});

alertDialog.setNeutralButton("No", new DialogInterface.OnClickListener()
{
    public void onClick(DialogInterface dialog, int which)
    {
         view.stopLoading();
    }
});

 return flag;
}

并就此提供反馈

更新:没有JS

设置WebView,如

WebView webView;
String webViewUrl = "http://www.google.com";
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setSupportZoom(true);
webView.loadUrl(webViewUrl);

然后实施WebViewClient,如:

boolean flag=true; 
webView.setWebViewClient(new WebViewClient() {
        ProgressDialog progressDialog;

        public boolean shouldOverrideUrlLoading(final WebView view,final String url) {        

        if (!url.contains("google")) {

                // Could be cleverer and use a regex
                // Open links in new browser
                //view.getContext().startActivity(
                    //  new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

                // Here we can open new activity

                flag=false;

            } else {

                // Stay within this webview and load url
                view.loadUrl(url);
                System.out.println("url: "+url);
                showAlert(url);
                flag=true;
            }

            return flag;

        }

        // Show loader on url load
        public void onLoadResource(WebView view, String url) {

            // if url contains string androidexample
            // Then show progress Dialog
            if (progressDialog == null && url.contains("google")) {

                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(WebView2.this);
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }
        }

        // Called when all page resources loaded
        public void onPageFinished(WebView view, String url) {

            try {
                // Close progressDialog
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

    });

现在,创建showAlert(String url)方法,如:

public void showAlert(final String url) {
    AlertDialog.Builder builder = new AlertDialog.Builder(WebView2.this);
    builder.setIcon(android.R.drawable.ic_dialog_alert);
    builder.setTitle("Go to this url?");
    builder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    if(!url.contains("google")) {
                          //flag=false;
                       }else{
                          Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                          startActivity(intent);
                          //flag=true;
                        }
                }
            });
    builder.setNeutralButton("No", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {
             webView.stopLoading();
        }
    });
    AlertDialog alert = builder.create();

    alert.show();

}