如何在Webview中显示android警报对话框?

Vas*_*asu 5 javascript android

我正在使用webview开发应用程序,但我不知道如何启用JavaScript以在webview中显示警报对话框.

我试过这个,但这对我不起作用.首先,我制作了webview和websettings对象,然后设置了以下参数.

    webSettings.setJavaScriptEnabled(true);

    webSettings.setBuiltInZoomControls(true);

    mWebView.requestFocusFromTouch();
Run Code Online (Sandbox Code Playgroud)

Jit*_*dra 20

根据你的代码,我认为你没有设置以下设置Webviewclient和Webcromeclient来在Webview中启用Javascript.

 mWebView.setWebViewClient(new WebViewClient());
 mWebView.setWebChromeClient(new WebChromeClient());
Run Code Online (Sandbox Code Playgroud)

然后使用以下代码加载Html页面.

 mWebView.loadUrl("file:///android_asset/"+Your_Html_Page);
Run Code Online (Sandbox Code Playgroud)

这是Webview中的Html部分.

<!DOCTYPE html>
<html>
<head>
    <script>
    function myFunction()
    {
        alert("Hello! I am an alert box!");
    }
    </script>
</head>
<body>

<input type="button" onclick="myFunction()" value="Show alert box" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

试试这可能会有所帮助.


bod*_*di0 6

WebChromeClient如果您想从 JavaScriptalert函数显示消息框,则不强制使用。您可以在 JavaScript 代码和客户端 Android 代码之间创建接口。

在下面的示例中,您的 JavaScript 代码可以调用 Android 代码中的一个方法来显示Dialog,而不是使用 JavaScript 的alert()函数。我发现这是显示警报的最方便和广泛支持的方式。

在您的 Android 应用程序中包含以下类:

文件:WebAppInterface.java

import android.content.Context;
import android.webkit.JavascriptInterface;

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a Message box from the web page */
    @JavascriptInterface
    public void androidAlert(String message) {
        DialogBox dbx = new DialogBox();
        dbx.dialogBox(message, "I get it", "",mContext);
    }
}
Run Code Online (Sandbox Code Playgroud)

文件:DialogBox.java

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

public class DialogBox {    
    public boolean dialogBox(String msg, String okButton, String cancelButton, final Context activity) {
        Dialog v = null;
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setMessage(msg);
        if (okButton != "") {
            alertDialogBuilder.setPositiveButton(okButton,
                    new DialogInterface.OnClickListener() {    
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) { /**/ }
                    });
        }    
        if (cancelButton != "") {
            alertDialogBuilder.setNegativeButton(cancelButton,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) { /**/ }
                    });
        }

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
        return true;
    }    
}
Run Code Online (Sandbox Code Playgroud)

接下来,绑定WebAppInterface类的JavaScript,在你运行WebViewaddJavascriptInterface()和命名接口Android

WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
Run Code Online (Sandbox Code Playgroud)

这将创建一个接口,调用AndroidWebView. 此时,您的 Web 应用程序可以访问 WebAppInterface该类。下面是一些 HTML 和 JavaScript,它们在用户单击按钮时使用新界面创建消息框:

<input type="button" value="Alert!" onClick="javascript:Android.androidAlert('It works!');" />
Run Code Online (Sandbox Code Playgroud)

单击按钮时,该Android接口用于调用该 WebAppInterface.androidAlert()方法。

一点警告:使用addJavascriptInterface()允许 JavaScript 控制您的 Android 应用程序。这可能是一个非常有用的功能危险的安全问题。因此,addJavascriptInterface()除非您编写了出现在WebView.