Android的:如何添加支持WebViewClient的JavaScript警告框? [英] Android:How to add support the javascript alert box in WebViewClient?

查看:77
本文介绍了Android的:如何添加支持WebViewClient的JavaScript警告框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜   我实现了使用像onUnhandledKeyEvent,shouldOverrideUrlLoading和more.If的webViewClient很多事情要添加到alertbox的支持则需要切换到WebChromeClient那么我不能做其他things.Any一个知道如何混合二者未来?
   我检查$ C $下javasript警告框<一href="http://lexandera.com/2009/01/adding-alert-support-to-a-webview/">http://lexandera.com/2009/01/adding-alert-support-to-a-webview/

Hi I implement the many things using the webViewClient like onUnhandledKeyEvent,shouldOverrideUrlLoading and more.If want to add the support for alertbox then need to switch to WebChromeClient then i can not do other things.Any one know how mix the both future?
I have check the code for javasript alert box at http://lexandera.com/2009/01/adding-alert-support-to-a-webview/


谢谢

推荐答案

刚刚实施WebViewClient和WebChromeClient都喜欢这个

just implement the WebViewClient and WebChromeClient both like this

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警告,确认和提示

then implement the WebChromeClient for javascript alert,confirm and prompt

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

 });

有关详细信息,请查看<一个href="http://$c$c.google.com/p/mosembro/source/browse/trunk/src/com/lexandera/mosembro/Mosembro.java">http://$c$c.google.com/p/mosembro/source/browse/trunk/src/com/lexandera/mosembro/Mosembro.java

这篇关于Android的:如何添加支持WebViewClient的JavaScript警告框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆