日期:2014-05-17  浏览次数:20717 次

用html5编写android程序
1:首先在 assets 文件夹下保存一个index.html的文件。
<script type="application/x-javascript">
    function drawText() {
      var canvas = document.getElementById("canvas");
      if (canvas.getContext) {
        var ctx = canvas.getContext("2d");
        var message = "First HTML5 page"

        ctx.fillStyle = "#FF0000";
        ctx.font = "30px serif";
var xPosition = 20;
var yPosition = 30;
       
        ctx.fillText (message,xPosition,yPosition);
      }
    }
  </script>

<body onload="drawText();">
   <canvas id="canvas" width="300" height="300"></canvas>
</body>

2:定义两个变量WebView mWebView;
String path = "file:///android_asset/index.html";
3:在oncreate里写上如下代码
mWebView = (WebView) findViewById(R.id.webview1);
mWebView.setBackgroundColor(Color.WHITE);
//mWebView.getSettings().setDefaultTextEncodingName(?"UTF-8");
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().supportMultipleWindows();
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.clearView();
mWebView.loadUrl(path);
ok了