根据用户选择覆盖webview网址

时间:2016-01-25 22:07:36

标签: java android webview alertdialog

我遇到了一种情况,我认为这是一个常见的情况,但我很难找出解决方案。我发现This与我的问题相似,但我不明白解决方案对他有何影响。任何人都可以指出正确的方法或解释答案,如果正确的话。

如果用户说yes,我只想覆盖webview的网址,否则不会覆盖,这是我的代码

@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等待用户响应,现在没有发生,并且最初立即返回布尔result[0],这是假的。

如果我遗漏任何东西,请指出我。

1 个答案:

答案 0 :(得分:0)

AlertDialog不会阻止UI线程 显示AlertDialog时,它不会阻止WebView继续使用它的新URL更新UI。

您最初需要阻止加载网址,然后在用户做出决定后再次致电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;
    }
}