我有一个简单的 html 页面:
<html>
<head>
<script >
function submit() {
alert('aa');
}
</script>
</head>
<body>
<form action = "form.html" method = "POST">
Label<inpt type = "text"></br>
Label2<inpt type = "text"></br>
Label3<inpt type = "text"></br>
Label4<inpt type = "text"></br>
Label5<inpt type = "text"></br>
<button type="button" id="no" onclick="submit();">Submit</button>
</form>
</body>
</html>
现在我想使用函数 submit 在 android 中显示 AlertDialog。这是我的代码:
final MyJavaScriptInterface myJavaScriptInterface
= new MyJavaScriptInterface(this);
formWebView.addJavascriptInterface(myJavaScriptInterface, "no");
formWebView.getSettings().setJavaScriptEnabled(true);
formWebView.loadUrl(url);
formWebView.getSettings().setBuiltInZoomControls(true);
和 MyJavaScriptInterface:
public class MyJavaScriptInterface {
Context mContext;
MyJavaScriptInterface(Context c) {
mContext = c;
}
public void submitForm(){
AlertDialog.Builder myDialog
= new AlertDialog.Builder(AmmsFormsWebViewActivity.this);
myDialog.setTitle("DANGER2!");
myDialog.setMessage("You can do what you want2!");
myDialog.setPositiveButton("ON", null);
myDialog.show();
}
}
但不起作用。我哪里有错误?我想检查是否从 webview 使用 submit() 方法我想在 android 中显示对话框。