用eclipse把h5打包成安卓apk
阿新 • • 發佈:2018-12-22
activity_main.xml程式碼,例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
在MainActivity中的程式碼如下:
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
webview=(WebView)findViewById(R.id.webview);
//設定WebView屬性,能夠執行JavaScript指令碼
webview.getSettings().setJavaScriptEnabled(true);
//設定渲染(必須)
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.setWebChromeClient(new WebChromeClient());
//載入URL內容
webview.loadUrl("http://webapp.cbd88.cn/");
//設定web檢視客戶端
webview.setWebViewClient(new MyWebViewClient());
}
//設定回退
public boolean onKeyDown(int keyCode,KeyEvent event)
{
if((keyCode==KeyEvent.KEYCODE_BACK)&&webview.canGoBack())
{
webview.goBack();
return true;
}
return super.onKeyDown(keyCode,event);
}
//web檢視客戶端
public class MyWebViewClient extends WebViewClient
{
public boolean shouldOverviewUrlLoading(WebView view,String url)
{
view.loadUrl(url);
return true;
}
}
}