Android:如何在WebViewClient中添加支持JavaScript警告框?

12

我使用webViewClient实现了许多功能,例如onUnhandledKeyEvent、shouldOverrideUrlLoading等。如果想要添加alertbox支持,则需要切换到WebChromeClient,这样我就不能做其他事情了。有没有人知道如何混合使用这两个未来技术?
我已经在http://lexandera.com/2009/01/adding-alert-support-to-a-webview/ 检查了JavaScript警告框的代码。


谢谢


嘿,Rubi,你不明白需要什么吗? - chikka.anddev
我想要在WebViewClient中添加JavaScript警告框支持,而不是使用WebChromeClient。如果你不明白的话,那就让我贴出代码。 - Sameer Z.
3个回答

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();
    }
}........

然后为JavaScript的alert、confirm和prompt实现WebChromeClient。

 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


2
一切都运行良好!还有一个细节:确认对话框必须是不可取消的,或者具有类似于提示的取消事件监听器。否则,它将冻结Web视图,因为它会等待结果。 - prcu

4

如果想添加对警告框(alertbox)的支持,需要切换到WebChromeClient,这时我就不能做其他事情了。

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


3

您需要为您的WebView设置WebChromeClient(这处理JavaScript对话框、网站图标、标题和进度)。

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

希望这有所帮助 :)

正是我想要的!谢谢。我不知道如何实现它来显示jsStuff。 - Ayyappa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接