我正在制作一个可在android webview中运行的应用程序。我面临一个奇怪的问题,我的JavaScript函数没有在<a></a>
onClick方法上调用。
这是我的html和JavaScript代码:
<html>
<script type="text/javascript" src="http://blue.xxxxxxxx.com/xxxxx/site/js/jquery.js"></script>
<script>
function openFileDialog()
{
//$("#file").click();
alert("Test");
}
</script>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<a href="javascript:;" onclick="openFileDialog();">Shujaat</a>
</body>
</html>
这是我的整个Java Webviews代码。
package com.example.findozerapp;
import my.functions.MyFunctions;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
Context context = MainActivity.this;
Activity activity = MainActivity.this;
WebView webView;
MyFunctions myFunctions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFunctions = new MyFunctions(activity);
webView = (WebView) findViewById(R.id.webView1);
configureWebview();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
webView.loadUrl("http://www.xxxxxxx.com/qadir/");
}
private void configureWebview() {
webView.setPadding(0, 0, 0, 0);
webView.setInitialScale(myFunctions.setWebViewScale());
webView.getSettings().setDefaultZoom(ZoomDensity.FAR);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setCacheMode(MODE_APPEND);
webView.setWebViewClient(new MyWebViewClient());
}
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
}
请注意,我已经在Webview中启用了javascript。
但它没有调用我的javascript函数openFileDialog()
。
还有一件事。每当我在默认的android浏览器中加载此网页时,它的运行效果都非常好。我做错了地方。请检查我的网络视图设置。
如果您将targetSdkVersion设置为17或更高,则必须将@JavascriptInterface批注添加到您希望网页代码可用的任何方法中(该方法也必须是公共的)。如果不提供注释,则在Android 4.2或更高版本上运行时,您的网页将无法访问该方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句