我正在使用webview开发应用程序,但我不知道如何启用JavaScript以在webview中显示警告对话框。
我曾尝试过这个,但这对我不起作用。 首先,我制作了webview和websettings对象 然后设置以下参数。
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
mWebView.requestFocusFromTouch();
答案 0 :(得分:18)
根据您的代码,我认为您没有设置以下设置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>
尝试这可能会有所帮助。
答案 1 :(得分:3)
如果要显示JavaScript WebChromeClient
功能的消息框,则不必使用alert
。您可以在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;
}
}
接下来,将WebAppInterface
类绑定到WebView
中使用addJavascriptInterface()
运行的JavaScript并命名接口Android
:
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
这为在Android
中运行的JavaScript创建了一个名为WebView
的界面。此时,您的网络应用可以访问 WebAppInterface
课程。下面是一些HTML和JavaScript,当用户单击按钮时,使用新界面创建一个消息框:
<input type="button" value="Alert!" onClick="javascript:Android.androidAlert('It works!');" />
点击该按钮,Android
界面用于调用 WebAppInterface.androidAlert()
方法。
一点警告:使用addJavascriptInterface()
可以让JavaScript控制您的Android应用。这可能是非常有用的功能或危险的安全问题。因此,除非您编写了addJavascriptInterface()
中显示的所有HTML和JavaScript,否则不应使用WebView
。
答案 2 :(得分: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" />
答案 3 :(得分:0)
如果你想从WebView中的事件中显示本机AlertDialog,那么这就是你在寻找Binding JavaScript code to Android code。
示例代码在此链接上可用。示例中显示Toast..u可以显示已发送的AlertDialog。
答案 4 :(得分:0)
要在你的webview上启用java脚本对话,可以试试这个...在下面的网址... URL