Android绑定JavaScript代码到Android代码不工作

Android Binding JavaScript code to Android code not Working?

本文关键字:Android 代码 工作 绑定 JavaScript      更新时间:2023-09-26

我已经使用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!')'"