我正在从我的WebView
. Toast
每当单击 中的按钮时,该方法就会执行WebView
。它工作正常,但是当我通过参数时它不起作用。当我传递一个参数时,该方法没有被执行。
这是我javascript
的strings.xml
-
<string name="details">
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>My HTML</title>
</head>
<body>
<h1>MyHTML</h1>
<p id="mytext">Hello!</p>
<input type="button" value="Say hello" onClick="showAndroidToast('Hello world!')" />
<script language="javascript">
function showAndroidToast(toast) {
AndroidFunction.showToast(toast);
}
function callFromActivity(msg){
document.getElementById("mytext").innerHTML = msg;
}
</script>
</body>
</html></string>
这是我的Activity's
代码-
String str = "<html><body>"
+ getString(R.string.details)
+ "</body></html>";
webview.addJavascriptInterface(new MyJavaScriptInterface(this),
"AndroidFunction");
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.loadDataWithBaseURL(null, str, "text/html", "utf-8", null);
这是我的MyJavaScriptInterface
课:
public class MyJavaScriptInterface {
Context mContext;
MyJavaScriptInterface(Context c) {
Log.d("tag", "cls");
mContext = c;
}
public void showToast(String toast) {//without the argument this method executes
Log.d("tag", "msg");
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}