class MyWebChromeClient extends WebChromeClient {
@Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(WebViewActivity.this, R.style.AlertDialog);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
double width = metrics.widthPixels * 0.9;
double height = metrics.heightPixels * 0.4;
AlertDialog alertDialog = builder.setMessage(message)
.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
})
.create();
alertDialog.show();
alertDialog.getWindow().setLayout((int)width, ViewGroup.LayoutParams.WRAP_CONTENT);
return true;
} }
需要在WebChromeClient中重写onJsConfirm方法,在里面使用自定义的AlertDialog就可以了。