如何将HTML/Javascript代码转换为可用于Eclipse中的Android应用程序

How to convert HTML/Javascript codes to be available for use for Android Application in Eclipse

本文关键字:用于 Eclipse 中的 应用程序 Android 转换 HTML 代码 Javascript      更新时间:2023-09-26

如何将这些转换为Java?

HTML:

<head>
<title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
</head>
<body onload="initialize()">
  <div>
    <input id="address" type="textbox" value="96818">
    <input type="button" value="Geocode" onclick="codeAddress()">
       <input id="coord" type="textbox" value="">
  </div>
</body>
</html>
JAVASCRIPT

 var geocoder;
function initialize() {
    geocoder = new google.maps.Geocoder();
}
function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode({
        'address': address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            $('#coord').val(results[0].geometry.location);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

我正在做一个Android应用程序项目,我想使用代码从邮政编码获得拉丁,所以我可以在谷歌地图上显示位置。

你可以使用WebView来实现。

WebView是Android中的一个小部件,它可以像web浏览器一样显示网站或html文件。WebView的核心与Chrome相同,所以如果你的网页在Chrome中运行良好,它也会在WebView中运行。

首先,把你的html代码和JS代码放到一个html文件中,然后把它放到你的Android应用的"/assets"文件夹中。

下面的代码将在屏幕上显示页面,并允许JS运行。

public class TestActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView webView=new WebView(this);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        String url = "file:///android_asset/test.html";
        webView.loadUrl(url);
        setContentView(webView);
    }
}