[英]Android Binding JavaScript code to Android code not Working?
我已经使用JavaScript
代码将android
这个文件用于此,但是我没有得到String value
为什么我要面对这个问题,任何人对此都有想法,请帮助我。
这是我的代码
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
String html = " <input type='button' value='Say hello' onClick='showAndroidToast('Hello Android!')' />" +
"<script type='text/javascript'> function showAndroidToast(toast) { Android.showToast(toast); }</script>";
webview.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
webview.addJavascriptInterface(new WebAppInterface(MainActivity.this), "Android");
和WebAppInterface
类
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Log.i(" ", " " + toast);
Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show();
}
}
这部分似乎不太可行:
'showAndroidToast('Hello Android!')'
您的报价将无法正确关闭。 使用转义的双引号代替外部的:
\"showAndroidToast('Hello Android!')\"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.