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