Android 如何处理 webview 确认对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2726377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle a webview confirm dialog?
提问by brockoli
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 中显示网页,网页上有一个按钮。当您单击该按钮时,应该会弹出一个确认对话框,但它没有显示在我的 WebView 中。如果我在 android 浏览器中访问同一个网页,它会弹出。任何人都知道如何处理来自 WebView 内网页的弹出对话框?
回答by brockoli
Ok, found the answer and here it is!
好的,找到了答案,就在这里!
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.
为了处理来自 WebView 中网页的弹出确认,您需要覆盖 WebChromeClient 中的 onJsConfirm 方法以将弹出窗口显示为 Android Alert 对话框。这是执行此操作的代码。
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;
}
}
Don't forget to set your WebChromeClient in your WebView...
不要忘记在您的 WebView 中设置您的 WebChromeClient...
mWebView.setWebChromeClient(new MyWebChromeClient());
Note.. this isn't my code, but I found it and it works perfectly for handling javascript confirmation dialogs in a WebView!
注意..这不是我的代码,但我找到了它,它非常适合处理 WebView 中的 javascript 确认对话框!
Cheers!
干杯!
回答by Pmc
Thanks Brockoli for the method. I needed this for Xamarin.Android
感谢 Brockoli 的方法。我需要这个用于 Xamarin.Android
public class MyWebChromeClient : WebChromeClient
{
private Context mContext;
private JsResult res;
public MyWebChromeClient(Context context)
{
mContext = context;
}
public override bool OnJsConfirm(WebView view, string url, string message, JsResult result)
{
res = result;
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.SetTitle("Confirm:");
builder.SetMessage(message);
builder.SetPositiveButton(Android.Resource.String.Ok, OkAction);
builder.SetNegativeButton(Android.Resource.String.Cancel, CancelAction);
builder.Create();
builder.Show();
return true;
//return base.OnJsConfirm(view, url, message, result);
}
private void CancelAction(object sender, DialogClickEventArgs e)
{
res.Cancel();
}
private void OkAction(object sender, DialogClickEventArgs e)
{
res.Confirm();
}
}
This back in the activity where webview is created (web_view)
这回到了创建 webview 的活动中 (web_view)
web_view.SetWebChromeClient(new MyWebChromeClient(this));