如何在Webview中显示安卓提示对话框?

6

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

我曾经尝试过这个方法,但对我来说并不起作用。 首先,我创建了webview和websettings对象, 然后设置以下参数。

    webSettings.setJavaScriptEnabled(true);

    webSettings.setBuiltInZoomControls(true);

    mWebView.requestFocusFromTouch();

https://dev59.com/_17Va4cB1Zd3GeqPLqS1 - Raghunandan
@Ganesh:你想展示Android原生的AlertDialog(http://developer.android.com/reference/android/app/AlertDialog.html)吗? - Tal Kanel
5个回答

24

根据你的代码,我认为你没有设置以下设置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>

尝试这个可能会有帮助。


太好了,这解决了我在显示JS警报方面遇到的问题,非常感谢。 - Mark Railton
当我添加了上述两行时,它可以工作。但是我该如何在警告消息顶部删除URL? - gnganapath

6

如果您想要从JavaScript的alert函数中显示消息框,不一定需要使用WebChromeClient。您可以在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);
    }
}

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

接下来,使用addJavascriptInterface()方法将WebAppInterface类与在你的WebView中运行的JavaScript代码进行绑定,并将接口命名为Android

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

这将为在 WebView 中运行的 JavaScript 创建一个名为 Android 的接口。此时,你的 Web 应用程序可以使用 WebAppInterface 类。以下是一些使用新接口创建消息框的 HTML 和 JavaScript 代码,在用户点击按钮时会触发:

<input type="button" value="Alert!" onClick="javascript:Android.androidAlert('It works!');" />
一按,Android界面就会调用 WebAppInterface.androidAlert() 方法。 需要注意的是:使用 addJavascriptInterface() 允许 JavaScript 控制您的 Android 应用程序。这可能是一个非常有用的功能,也可能是一个危险的安全问题。因此,除非您编写了出现在 WebView 中的所有 HTML 和 JavaScript,否则不应该使用 addJavascriptInterface()

1
为了在您的webview上启用JavaScript对话框,您可以尝试这个答案

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" />

0
如果你想要在 WebView 中的事件中显示本机 AlertDialog,那么这就是你需要的将 JavaScript 代码绑定到 Android 代码
示例代码可在此链接上找到。链接中的示例会显示 Toast,你可以改为显示 AlertDialog。

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