1.Layout中添加Webview控件
<WebView
android:id="@+id/my_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
2.设置支持Javascript
WebSettings myWebSettings = mMyWebView.getSettings();
myWebSettings.setJavaScriptEnabled(true);
3.支持超链接,而不是使用浏览器打开新连接
private class MyWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
//return super.shouldOverrideUrlLoading(view, url);
view.loadUrl(url);
return true;
}
}
mMyWebView.setWebViewClient(new MyWebViewClient());
4.点击系统的后退按钮返回上一个连接,而不是结束自己
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK && mMyWebView.canGoBack()) {
mMyWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
在AndroidManifest添加<uses-permission android:name="android.permission.INTERNET" />权限