WebView组件支持直接加载网页,能够将其视为一个浏览器,要实现该功能,具体步骤以下:html
webview.xmljava
<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>android
WebViewActivity.javaweb
public class WebViewActivity extends Activity{
private WebView webView;
private AlertDialog alertDialog;
private ProgressDialog progressBar;
jQuery datatables使用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
//加载WebView
initWebView();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
class MyWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if(progressBar.isShowing()){
progressBar.dismiss();
}
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(WebViewActivity.this, "网页加载出错!", Toast.LENGTH_LONG);
alertDialog.setTitle("ERROR");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialog.show();
}
}
protected void initWebView(){
//设计进度条
progressBar = ProgressDialog.show(WebViewActivity.this, null, "正在进入网页,请稍后…");
//得到WebView组件
webView = (WebView) this.findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.baidu.com");
alertDialog = new AlertDialog.Builder(this).create();
//设置视图客户端
webView.setWebViewClient(new MyWebViewClient());
}
}浏览器
最后,须要在**Manifest.xml中添加访问互联网的权限,不然不能显示:ide
<uses-permission android:name="android.permission.INTERNET"/>ui