
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="webviewtest.yweizheng.com.webviewtest.MainActivity"> <WebView android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="fill_parent" /></RelativeLayout>package com.imooc.android_webview;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;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.LayoutInflater;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;import android.widget.Toast;import android.os.Build;public class MainActivity extends Activity { private String url = "http://2014.QQ.com/"; private WebView webView; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.web); // Uri uri = Uri.parse(url); //url為你要鏈接的地址 // Intent intent =new Intent(Intent.ACTION_VIEW, uri); // startActivity(intent); init(); } private void init() { // TODO Auto-generated method stub webView = (WebView) findViewById(R.id.webView); // WebView加載本地資源 // webView.loadUrl("file:///android_asset/example.html"); // WebView加載web資源 webView.loadUrl(url); // 覆蓋WebView默認通過第三方或者是系統瀏覽器打開網頁的行為,使得網頁可以在WebVIew中打開 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //返回值是true的時候控制網頁在WebView中去打開,如果為false調用系統瀏覽器或第三方瀏覽器去打開 view.loadUrl(url); return true; } //WebViewClient幫助WebView去處理一些頁面控制和請求通知 }); //啟用支持javaScript WebSettings settings = webView.getSettings(); settings.setJavascriptEnabled(true); //WebView加載頁面優先使用緩存加載 settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub //newProgress 1-100之間的整數 if(newProgress==100) { //網頁加載完畢,關閉ProgressDialog closeDialog(); } else { //網頁正在加載,打開ProgressDialog openDialog(newProgress); } } private void closeDialog() { // TODO Auto-generated method stub if(dialog!=null&&dialog.isShowing()) { dialog.dismiss(); dialog=null; } } private void openDialog(int newProgress) { // TODO Auto-generated method stub if(dialog==null) { dialog=new ProgressDialog(MainActivity.this); dialog.setTitle("正在加載"); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(newProgress); dialog.show(); } else { dialog.setProgress(newProgress); } } }); } //改寫物理按鍵——返回的邏輯 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode==KeyEvent.KEYCODE_BACK) { //Toast.makeText(this, webView.getUrl(), Toast.LENGTH_SHORT).show(); if(webView.canGoBack()) { webView.goBack();//返回上一頁面 return true; } else { System.exit(0);//退出程序 } } return super.onKeyDown(keyCode, event); }}AndroidMainfest.xml
新聞熱點
疑難解答