6

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

我曾经尝试过这个,但它不适合我。首先,我制作了 webview 和 websettings 对象,然后设置了以下参数。

    webSettings.setJavaScriptEnabled(true);

    webSettings.setBuiltInZoomControls(true);

    mWebView.requestFocusFromTouch();
4

5 回答 5

22

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

 mWebView.setWebViewClient(new WebViewClient());
 mWebView.setWebChromeClient(new WebChromeClient());

然后使用以下代码加载您的 Html 页面。

 mWebView.loadUrl("file:///android_asset/"+Your_Html_Page);

这是您的 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>

试试这可能会有所帮助。

于 2013-05-11T05:46:27.143 回答
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);
    }
}

文件: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;
    }    
}

接下来,将类绑定到在您的withWebAppInterface中运行的 JavaScript并命名接口:WebViewaddJavascriptInterface()Android

WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

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

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

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

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

于 2016-07-29T11:22:58.220 回答
1

要在您的 webview 上启用 JavaScript 对话,您可以试试这个答案

于 2017-08-28T11:55:49.503 回答
0

试试这个代码:

public OnClickListener imageButtonViewOnClickListener = new OnClickListener() {
public void onClick(View v) {

LayoutInflater inflater = LayoutInflater.from(MyActivity.this);

// error here
View alertDialogView = inflater.inflate(R.layout.alert_dialog_layout, null);

WebView myWebView = (WebView) findViewById(R.id.DialogWebView);
myWebView.loadData(webContent, "text/html", "utf-8");
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setView(alertDialogView);

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
}).show();
}
};

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">

<WebView android:id="@+id/DialogWebView" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
于 2013-05-11T05:41:11.863 回答
0

如果您想从 WebView 中的事件显示本机 AlertDialog,那么这就是您正在寻找的Binding JavaScript code to Android code

此链接上提供了示例代码..那里的示例显示了一个 Toast..u 可以显示一个 AlertDialog insted。

于 2013-05-11T05:45:57.563 回答