在这种情况下如何将数据从 Javascript 传递到 Android WebView?

Viv*_*kar 4 android

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Main();

}
public void Main()
{
    _linearLayout = new LinearLayout(this);
    _webview = new WebView(this);
    _linearLayout.addView(_webview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    setContentView(_linearLayout);

    _webview.getSettings().setJavaScriptEnabled(true);
    _webview.getSettings().setPluginsEnabled(true);
    _webview.getSettings().setAllowFileAccess(true);

    _webview.setWebChromeClient(new WebChromeClient());
    _webview.addJavascriptInterface(this, "Question");
    _webview.loadData(GetHTML(), "text/html", "utf-8");


}

public String GetHTML()
{   
    String HTML = ""
        + "<HTML>"
        + "<HEAD>"
        + "<TITLE>Radio Button onClick Handler</TITLE>"
        + "<SCRIPT LANGUAGE=\"JavaScript\">"
        +"function function1(colors) {" 
        +"var col = (colors.options[colors.selectedIndex].value);"
        +" if (col) {"
        +"  document.bgColor = col;"

        +"   } "
        +"</script>"
        + "</HEAD>"
        + "<BODY>"
        +"<form>"
        +"<b> Hello </b>"
        //+"<select name=\"colors\" onChange=\"window.Question.function1(this);\">"
        +"<select name=\"colors\" onChange=\"window.Question.OnJsClick_SelectedItem(' string value');\">"
           +"<option value=\"white\" selected>White</option>"
           + "<option value=\"cyan\">Cyan</option>"
           + "<option value=\"ivory\">Ivory</option>"
           + "<option id=\"myO\" value=\"blue\">Blue</option>"

        +"</select>"
        +"</form>"
        + "</BODY>"
        + "</HTML>";

    return HTML;
}

public void OnJsClick_SelectedItem(final String str)
{
    mHandler.post(new Runnable()
    {
        //@Override
        public void run()
        {
            getValue(str);
        }
    });
}

public String getValue(String str)
{
    _webview.loadUrl("javascript:function1(colors)");
    Toast.makeText(this, "Under getValue " + str, Toast.LENGTH_SHORT).show();
    return str;

}
}
Run Code Online (Sandbox Code Playgroud)

请帮帮我。

Ris*_*abh 5

您可以将代码作为 HTML 页面存储在 asset 文件夹中,并使用 webview 的加载 URL 方法来显示网页。

mWebView.loadUrl("file:///android_asset/index.html");
Run Code Online (Sandbox Code Playgroud)

根据评论修改

启用 JavaScript

 WebView webView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
Run Code Online (Sandbox Code Playgroud)

这是一些 HTML 和 JavaScript,当用户单击按钮时,它们会使用新界面创建一条 toast 消息

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>
Run Code Online (Sandbox Code Playgroud)

将其添加到您的 Java 代码中

public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)