如何直接从 Android 中的活动调用 javascript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8654386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a javascript function directly from an activity in Android?
提问by Smitha
I wanted to know if there is any way that I can call a JavaScript function from native Android Activity.
我想知道是否有任何方法可以从原生 Android Activity 调用 JavaScript 函数。
I came across:
我碰到:
webView.loadUrl("javascript:hello()");
But it did not work for me.
但它对我不起作用。
Also, how will Android know in which html page does this JavaScript function reside?
另外,Android 如何知道此 JavaScript 函数驻留在哪个 html 页面中?
回答by Talha
Android can only call the javascript method if an html page is currently loaded in webView
如果当前在 webView 中加载了 html 页面,Android 只能调用 javascript 方法
webView.loadUrl("javascript:hello()");
will call hello method writen in the html page only if the page containing this method is currently loaded in the webview control
仅当 webview 控件中当前加载了包含此方法的页面时,才会调用 html 页面中写入的 hello 方法
first call
第一个电话
webview.loadUrl("Your html page url");
then call
然后打电话
webView.loadUrl("javascript:hello()");
回答by tim.paetz
In case anyone has any issues with this like I did, you need to call your javascript function after the page has finished loading otherwise the javascript won't be called:
如果有人像我一样遇到任何问题,您需要在页面加载完成后调用您的 javascript 函数,否则将不会调用 javascript:
webviewer.loadUrl("file:///android_asset/mypage.html");
webviewer.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url)
{
webviewer.loadUrl("javascript:hello()");
}
});
回答by Akilan
Before Using JavaScript, first you have to check Javascript is enabled or not in your application using "getJavaScriptEnabled () " (Its default FALSE). And loadUrl() method only used to load the current page only. Its does not do anything related to JavaScript.
在使用 JavaScript 之前,首先您必须使用“getJavaScriptEnabled ()”(默认为 FALSE)检查您的应用程序中是否启用了 Javascript。而 loadUrl() 方法仅用于加载当前页面。它不做任何与 JavaScript 相关的事情。
EXAMPLE:
例子:
Setting up WebView
设置 Web 视图
//initialization of webview
//初始化webview
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
Loading HTML in WebView
在 WebView 中加载 HTML
webView.loadUrl("file:///android_asset/testhtml.html");
Setting WebChromeClient to webView
将 WebChromeClient 设置为 webView
webView.setWebChromeClient(new MyJavaScriptChromeClient());
The MyJavaScriptChromeClient class Here we have to override onJsAlert() method to handle JavaScript Alert Function.
MyJavaScriptChromeClient 类 这里我们必须覆盖 onJsAlert() 方法来处理 JavaScript 警报功能。
private class MyJavaScriptChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message,final JsResult result) {
//handle Alert event, here we are showing AlertDialog
new AlertDialog.Builder(MainWebViewActivity.this)
.setTitle("JavaScript Alert !")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do your stuff
result.confirm();
}
}).setCancelable(false).create().show();
return true;
}
}
My Html File : testhtml.html :
我的 Html 文件:testhtml.html:
<html>
<body >
<div onclick="alert('hello')"> Click Me !! </div>
</body>
</html>
How it Works ? When the text "Click Me !!" on the WebView is clicked, The android function onJsAlert(WebView view, String url, String message,final JsResult result) is called. The parameter to alert is copied to message parameter of onJsAlert function. And rest of the handling is done there. Here we are displaying a AlertDialog .
怎么运行的 ?当文本“点击我!!” 单击 WebView 上的 android 函数 onJsAlert(WebView view, String url, String message,final JsResult result) 被调用。alert 的参数被复制到 onJsAlert 函数的 message 参数中。其余的处理在那里完成。这里我们显示一个 AlertDialog 。