在android webview中使用javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10472839/
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
using javascript in android webview
提问by Common
I'm trying to start an activity from a javascript interface in my webview. The example shows a toast. How could i call a class instead of a toast?
我正在尝试从我的 web 视图中的 javascript 界面开始一项活动。该示例显示了吐司。我怎么能叫一个班而不是一个祝酒词?
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();
}
}
this for the html page.
这是 html 页面。
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
回答by Akshay
You have to first register the JavaScriptInterface on your webview. JavaScriptInterFace can be a inner class as shown below. This class will have a function that you can call from html page( via javaScript ) and inside this function you can write code to change activity.
你必须首先在你的 webview 上注册 JavaScriptInterface。JavaScriptInterFace 可以是一个内部类,如下所示。此类将有一个函数,您可以从 html 页面(通过 javaScript )调用该函数,在此函数中,您可以编写代码来更改活动。
Here is the working solution for you:
这是适合您的工作解决方案:
public class JavascriptInterfaceActivity extends Activity {
/** Called when the activity is first created. */
WebView wv;
JavaScriptInterface JSInterface;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wv = (WebView)findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
// register class containing methods to be exposed to JavaScript
JSInterface = new JavaScriptInterface(this);
wv.addJavascriptInterface(JSInterface, "JSInterface");
wv.loadUrl("file:///android_asset/myPage.html");
}
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
@android.webkit.JavascriptInterface
public void changeActivity()
{
Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class);
startActivity(i);
finish();
}
}
}
Here is the html page
这是html页面
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
JSInterface.changeActivity();
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>
Hope this helps...
希望这可以帮助...
回答by goseib
You also need to add the @android.webkit.JavascriptInterfaceannotation on top of your changeActivity
method in your android code, should you run on Android 4.2 or higher.
See this linkfor more.
如果您在 Android 4.2 或更高版本上运行,您还需要在您的 android 代码中的方法顶部添加@android.webkit.JavascriptInterface注释changeActivity
。请参阅此链接了解更多信息。