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

Sam*_* Z. 12 android

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


谢谢

Sam*_* Z. 26

像这样实现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();
    }
}........
Run Code Online (Sandbox Code Playgroud)

然后实现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;
        };

 });
Run Code Online (Sandbox Code Playgroud)

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

  • 一切正常!还有一个细节:确认对话框必须是不可取消的,或者取消事件监听器类似于提示.其他方式它会冻结webview,因为它会等待结果. (2认同)