从Java-Android调用Javascript
我有一个使用android webview的应用程序。我试图从Java调用javascript。我有一行,下面我希望显示从Java-Android调用Javascript,java,javascript,android,Java,Javascript,Android,我有一个使用android webview的应用程序。我试图从Java调用javascript。我有一行,下面我希望显示hello world,但是webview变为空白 webView = (WebView) findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("javascript:document.write('hello world')"); 我的代
hello world
,但是webview变为空白
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("javascript:document.write('hello world')");
我的代码是正确的还是遗漏了什么
使用
document.write进行显示仅用于调用JS函数的测试目的。首先将字符串从“hello”更改为“hello world”:-)
我试过这个,它是有效的
活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("javascript:document.write('hello world')");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
试试这个方法
wview.loadUrl(“file:///android_asset/index.html");
**index.html**
复制assests文件夹中的index.html
在此处插入标题
你好,世界
我使用了document.write
来编写hello world
仅用于测试目的。我们的要求是调用JS函数,而不仅仅是显示hello world
。wat的您的要求可以满足吗?我已经尝试了您的答案,但没有成功。。你能帮我回答我的问题吗?
Try this way
wview.loadUrl("file:///android_asset/index.html");
**index.html**
copy index.html in assests folder
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>