からメソッドを呼び出してい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();
}
}