Android:如何添加支持WebViewClient中的javascript警报框?

我使用webViewClient实现了很多东西,比如onUnhandledKeyEvent,shouldOverrideUrlLoading等等。如果想添加对alertbox的支持,那么需要切换到WebChromeClient然后我不能做其他的事情。任何人都知道如何混合这两个未来?
我在http://lexandera.com/2009/01/adding-alert-support-to-a-webview/查看了javasript警报框的代码。

谢谢

  • Android:如何从Samsung Galaxy Tab获取数据/ anr / traces.txt?
  • 运行模拟器时“com.android.exchange.ExchangeService泄漏...”错误
  • Android如何将Camera或Gallery Intent一起调用
  • 复制保护陷阱
  • Objective-C和Android
  • onSaveInstanceState限制?
  • 使用Facebook的FirebaseUI身份validation无效
  • 如何检测从横向到横向180度的屏幕旋转?
  • 像这样实现WebViewClient和WebChromeClient

    webView = (WebView) findViewById(R.id.webview); webView.getSettings().setJavaScriptEnabled(true); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); progressBar = new ProgressDialog(this); progressBar.setCancelable(true); progressBar.setMessage("Loading..."); progressBar.show(); final Context mapp = this; webView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.i("TEST", "Processing webview url click..."); // to kill activity view.loadUrl(url); return true; } public void onPageFinished(WebView view, String url) { Log.i("TEST", "Finished loading URL: " + url); if (progressBar.isShowing()) { progressBar.dismiss(); } }........ 

    然后实现WebChromeClient的javascript警报,确认并提示

      webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) { new AlertDialog.Builder(mapp) .setTitle(R.string.title_dialog_alert) .setMessage(message) .setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }).setCancelable(false).create().show(); return true; } @Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { new AlertDialog.Builder(mapp) .setTitle(R.string.title_dialog_confirm) .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; } @Override public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) { final LayoutInflater factory = LayoutInflater.from(mapp); final View v = factory.inflate(R.layout.javascript_prompt_dialog, null); ((TextView)v.findViewById(R.id.prompt_message_text)).setText(message); ((EditText)v.findViewById(R.id.prompt_input_field)).setText(defaultValue); new AlertDialog.Builder(mapp) .setTitle(R.string.title_dialog_prompt) .setView(v) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String value = ((EditText)v.findViewById(R.id.prompt_input_field)).getText().toString(); result.confirm(value); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { result.cancel(); } }) .setOnCancelListener( new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { result.cancel(); } }) .show(); return true; }; }); 

    有关详细信息,请查看http://code.google.com/p/mosembro/source/browse/trunk/src/com/lexandera/mosembro/Mosembro.java

    如果想添加对alertbox的支持,那么需要切换到WebChromeClient然后我不能做其他事情。

    在大多数编程语言中,您可以拥有多个对象,包括Java。 因此,您将WebViewClient WebChromeClient作为单独的对象。

    您需要为WebView设置WebChromeClient (它处理JavaScript对话框,favicon,标题和进度)。

     WebView wView = new WebView(this){ @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { // TODO Auto-generated method stub Log.i("my log","Alert box popped"); return super.onJsAlert(view, url, message, result); } }; setContentView(wView); wView.getSettings().setJavaScriptEnabled(true); WebChromeClient cClient = new WebChromeClient(); wView.setWebChromeClient(cClient); wView.loadUrl("file:///android_asset/" + yourHtmlFile); 

    希望能帮助到你 :)

     WebView wView = new WebView(this){ @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { // TODO Auto-generated method stub Log.i("my log","Alert box popped"); return super.onJsAlert(view, url, message, result); } }; setContentView(wView); wView.getSettings().**strong text**setJavaScriptEnabled(true); WebChromeClient cClient = new WebChromeClient(); wView.setWebChromeClient(cClient); wView.loadUrl("file:///android_asset/" + yourHtmlFile);