3

我有以下测试 Android 应用程序。

public class TestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        WebView webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/www/test.html");        
   }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sudoku, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Need to call javascript function testFun() here (see test.html)
  }
}

test.html 的代码

<!doctype html>
<html>
  <head>
  <title>Test</title>
  <meta charset="UTF-8" />
  <script type="text/javascript">
    function testFun()
     {
       alert('Hi');
    }
  </script
  <body>
        <button type="button" onclick="testFun()">Test</button>
  </body>
</html>

我阅读了有关在 javascript http://developer.android.com/guide/webapps/webview.html中调用 android 函数的信息

但无法从 android 调用 javascript 函数(菜单项单击)。

4

1 回答 1

7

你有没有试过这个。

webView.loadUrl("javascript:testFun()");
于 2012-09-20T09:19:50.063 回答