在Android webview中没有调用Javascript函数

Qad*_*ain 5 javascript java android webview

我正在制作一个在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> 
Run Code Online (Sandbox Code Playgroud)

这是我的整个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);
    }
}

 }
Run Code Online (Sandbox Code Playgroud)

请注意我已在我的webview中启用了javascript.

但它没有调用我的javascript函数openFileDialog().

还有一件事.每当我在默认的android浏览器中加载我的这个网页时,它的工作完美.哪里我做错了.请检查我的webview设置.

小智 3

如果您已将 targetSdkVersion 设置为 17 或更高,则必须将 @JavascriptInterface 注释添加到您希望网页代码可用的任何方法(该方法也必须是公共的)。如果您不提供注释,则在 Android 4.2 或更高版本上运行时,您的网页将无法访问该方法。