如何处理的WebView确认对话框? [英] How to handle a webview confirm dialog?

查看:133
本文介绍了如何处理的WebView确认对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显示在web视图的网页,并在网页上,有一个按钮。当您单击按钮,应该是一个确认对话框弹出,但它并不在我的WebView显示。它不会弹出,如果我去同一个网页在Android浏览器。任何人都知道如何处理弹出对话框从网页降临到你的WebView里面?

I'm displaying a webpage in a WebView and on the webpage, there is a button. When you click the button, a confirmation dialog is supposed to popup, but it doesn't show in my WebView. It does popup if I go to the same webpage in the android browser. Anyone know how to handle popup dialogs coming from a webpage inside your WebView?

推荐答案

好了,找到了答案,就是这样!

Ok, found the answer and here it is!

为了处理弹出确认从网页进来你的web视图,你需要重写onJsConfirm方法WebChromeClient显示弹出作为一个Android警报对话框。这里是code这样做。

In order to handle a popup confirmation coming from a webpage in your WebView, you need to override the onJsConfirm method in WebChromeClient to display the popup as an Android Alert dialog. Here is the code to do so.

final Context myApp = this; 
final class MyWebChromeClient extends WebChromeClient {
    @Override
    public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
        new AlertDialog.Builder(myApp)
        .setTitle("App Titler")
        .setMessage(message)
        .setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                result.confirm();
            }
        })
        .setNegativeButton(android.R.string.cancel,
                new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                result.cancel();
            }
        })
        .create()
        .show();

        return true;
    }
}

不要忘记设置你的WebChromeClient在你的web视图...

Don't forget to set your WebChromeClient in your WebView...

    mWebView.setWebChromeClient(new MyWebChromeClient());

请注意...这不是我的code,但我发现它和它的作品完美地处理JavaScript的确认对话框中的WebView!

Note.. this isn't my code, but I found it and it works perfectly for handling javascript confirmation dialogs in a WebView!

干杯!

这篇关于如何处理的WebView确认对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆