我有一个简单的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>现在我想在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();
}
}但不起作用。我哪里有个bug?我想检查一下是否在webview中使用了submit()方法,我想在android中显示对话框。
发布于 2013-06-13 21:44:13
因为当您将JS接口添加到webview时,JS接口的“名称”是no,并且JS接口中的方法是submitForm(),所以javascript应该调用no.submitForm();来让接口捕获它。
https://stackoverflow.com/questions/17088262
复制相似问题