html5 app实例,html5 app 简单实例

一、编写一个HTML5 App

启动Eclipse,然后在菜单“File”下选择“New > Android Project”。

在项目根目录下,创建两个新目录:

/libs

/assets/www

复制phonegap.js(从PhoneGap解压缩后的Android目录中,将解压缩后的带版本号的js文件名修改为phonegap.js)到/assets/www。

复制phonegap.jar(从PhoneGap解压缩后的Android目录中,将解压缩后的带版本号的jar文件名修改为phonegap.jar)到/libs。

复制xml整个目录(从PhoneGap解压缩后的Android目录中,包括一个plugins.xml)到/res。【v1.0 rc2以上】

对Eclipse的src文件夹中的主要Java文件进行少量调整:(如下图)

将class的继承由Activity改为DroidGap

将setContentView()替换为super.loadUrl(“file:///android_asset/www/index.html”);

添加import com.phonegap.*;

移除import android.app.Activity;

在这里你可能会遇到Eclipse找不到phonegap.jar的错误。在这种情况下,右键单击/libs文件夹找到Build Paths/ > Configure Build Paths。然后在Libraries标签页中添加phonegap.jar到项目中。如果Eclipse不是很稳定,你需要点击F5刷新一次项目。

右键单击AndroidManifest.xml并选择Open With > Text Editor。

将下面的权限设置拷贝到versionName之后(这个可以根据APP的具体情况而定):

android:anyDensity="true"

android:largeScreens="true"

android:normalScreens="true"

android:resizeable="true"

android:smallScreens="true" />

在AndroidManifest的activity标签中添加android:configChanges="orientation|keyboardHidden"。

在 “/assets/www”目录中新建文件“index.html”,并粘贴如下代码:

小地盘

你好, 小地盘。

最后部署运行程序。

d814226f0ece58c46c04b039d8b0a48f.png

二、测试

MainActivity.java

package com.example.html5test;

import android.os.Bundle;

import com.phonegap.DroidGap;

public class MainActivity extends DroidGap {

String url = "file:///android_assets/www/index.html";

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.loadUrl(url);

}

}

测试结果:

4c7d01505fee03c162cd92e2e9b3c055.png

54807280dcf255650ff13461b213d465.png

修改MainActivity.java

package com.example.html5test;

import android.app.Activity;

import android.os.Bundle;

import android.webkit.WebSettings;

import android.webkit.WebView;

public class MainActivity extends Activity {

WebView mWebView;

String url = "file:///android_assets/www/index.html";

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.fragment_main);

WebView mWebView = (WebView) this.findViewById(R.id.webid);

mWebView.loadUrl(url);

WebSettings webSettings = mWebView.getSettings();

webSettings.setJavaScriptEnabled(true);

}

}

修改fragment_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.html5test.MainActivity$PlaceholderFragment" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

android:id="@+id/webid"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="100dp" />

测试结果:

111b6dd3fec05c4f6ec992f3d9c0d780.png

三、引用参考

具体问题列出来了,还没有解决,大牛们一起讨论帮忙解决。

fastcry.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值