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

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

问题描述

我在 WebView 中显示网页,网页上有一个按钮.当您单击该按钮时,应该会弹出一个确认对话框,但它没有显示在我的 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?

推荐答案

好的,找到答案了!

为了处理来自 WebView 中网页的弹出确认,您需要覆盖 WebChromeClient 中的 onJsConfirm 方法以将弹出窗口显示为 Android 警报对话框.这是执行此操作的代码.

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;
    }
}

不要忘记在您的 WebView 中设置您的 WebChromeClient...

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

    mWebView.setWebChromeClient(new MyWebChromeClient());

注意..这不是我的代码,但我发现它非常适合处理 WebView 中的 javascript 确认对话框!

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天全站免登陆