簡單的頁面跳轉
package com.example.webtest;
import java.security.PublicKey;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private String url = "http://2014.qq.com/";
private ProgressDialog progressDialog;
private WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
web = (WebView) findViewById(R.id.web);
// Uri uri = Uri.parse(url);
// URL:統一資源定位符 也就是網址 例如 http://www.microsoft.com/
// URI:通用資源標志符 http://www.acme.com/support/suppliers.htm
// Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// startActivity(intent);
init();
/*
* Uri uri = Uri.parse(url); // url為你要的鏈接 Intent intent = new
* Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
*/
}
private void init() {
// TODO Auto-generated method stub
// Log.d("tag", "DAYIN");
web.loadUrl(url);
// web.loadUrl("file:///android_asset/1.html");
web.setWebViewClient(new WebViewClient() {
// 覆蓋webView默認通過第三方或者是系統瀏覽器打開網頁的行為,使得網頁可以在WebView中打開
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
// 返回值是TRUE的時候控制網頁在webview中打開,如果為false的時候則調用系統瀏覽器或者第三方瀏覽器打開
return super.shouldOverrideUrlLoading(view, url);
}
});
// Webviewclient幫助webview去處理一些頁面控制和請求通知。
WebSettings settings = web.getSettings();
settings.setJavaScriptEnabled(true);// 使得手機自動適應網頁
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // webvie加載頁面優先使用緩存加載
web.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
super.onProgressChanged(view, newProgress);
if (newProgress == 100) {
// 網頁加載完畢, 關閉progressDialog
closeDialog();
} else {
openDialog(newProgress);
}
}
private void openDialog(int newProgress) {
// TODO Auto-generated method stub
if (progressDialog == null) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("正在加載中");
progressDialog
.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(newProgress);
progressDialog.show();
} else {
progressDialog.setProgress(newProgress);// 顯示新的進度
}
}
private void closeDialog() {
// TODO Auto-generated method stub
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();// 取消顯示
progressDialog = null;
}
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 改寫物理按鍵--返回的邏輯
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (web.canGoBack()) {
web.goBack();
return true;// 在javaScript里,return有終止函數的執行和傳遞數值,兩種功能。
// return false 就是返回假值,從而終止了函數的執行
// return true 就是返回真值,從而傳遞數值,繼續執行函數
} else {
System.exit(0);
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
web.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <WebView android:id="@+id/web" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>