微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

|
@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;

}
}
请帮帮我。     

解决方法

        您应该使用这样的东西:
<select name=\"colors\"
    onChange=\"Question.OnJsClick_SelectedItem(this.options[this.selectedIndex].text)\">
    ,        您可以将代码作为HTML页面存储在Assets文件夹中,并使用URL的load URL方法显示该页面。
mWebView.loadUrl(\"file:///android_asset/index.html\");
根据评论进行编辑 启用JavaScript
 WebView webView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JavaScriptInterface(this),\"Android\");
这是一些HTML和JavaScript,当用户单击按钮时,它们会使用新界面使用Toast消息创建Toast消息
<input type=\"button\" value=\"Say hello\" onClick=\"showAndroidToast(\'Hello Android!\')\" />

<script type=\"text/javascript\">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>
在您的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();
    }
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。