如何直接从Android中的活动调用javascript函数? [英] How to call a javascript function directly from an activity in Android?

查看:69
本文介绍了如何直接从Android中的活动调用javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以从原生Android Activity调用JavaScript函数。

I wanted to know if there is any way that I can call a JavaScript function from native Android Activity.

我遇到过:

webView.loadUrl("javascript:hello()");

但它对我不起作用。

此外,Android如何知道这个JavaScript函数驻留在哪个html页面?

Also, how will Android know in which html page does this JavaScript function reside?

推荐答案

如果当前在webView中加载了html页面,Android只能调用javascript方法

Android can only call the javascript method if an html page is currently loaded in webView

webView.loadUrl("javascript:hello()");

只有在当前加载包含此方法的页面时才会调用html页面中的hello方法webview控件

will call hello method writen in the html page only if the page containing this method is currently loaded in the webview control

第一次调用

webview.loadUrl("Your html page url");

然后致电

webView.loadUrl("javascript:hello()");

这篇关于如何直接从Android中的活动调用javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆