【问题标题】:How to call javascript function and get return value from android? [duplicate]如何调用javascript函数并从android获取返回值? [复制]
【发布时间】:2014-02-12 06:00:45
【问题描述】:

在这段代码中,我返回一个字符串。我想将此字符串存储在 android 活动中。如何调用 JavaScript 函数并从 android 活动中获取返回值?

 <script type="text/javascript">
        function gettext(){
            return 'http://192.168.1.11/bmahtml5/images/specs_larg_2.png';
        };
        function button_clicked(){
            window.location='ios:nativecall';
        };
 </script>

【问题讨论】:

  • 我可以知道你必须使用它吗??
  • 实际上我加载了 html 页面,当我点击某个部分时,我必须调用返回图像链接的 thi gettext 函数,并将此图像存储在特定变量中并在图像视图中显示图像。跨度>

标签: java javascript android


【解决方案1】:

这是一个简单的:

WebAppInterface.java

public class WebAppInterface
 {
        Context mContext;

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

        public void storeText(String text)
        {
            //TODO  save text
            Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
        }
    }

WebView webview=(WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new WebAppInterface(this), "Android");

现在你可以在html中调用这个方法了,看看吧

<script type="text/javascript">

    function saveFunction(text) 
    {
        Android.storeText(text);
    }
</script>

【讨论】:

  • 嗨 thnks 我在你的脚本函数 android.stroretext 函数调用中尝试了这个代码,在我的脚本函数中只返回字符串值我怎样才能得到这个字符串
  • 试试看,可以执行动作,但不返回值。 webview.loadUrl("javascript:gettext()");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 2020-12-03
  • 2011-06-30
  • 2013-10-24
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
相关资源
最近更新 更多