WebView的使用

MainActivity  

1
package webdemo.example.administrator.webdemo; 2 3 import android.app.Activity; 4 import android.app.ProgressDialog; 5 import android.content.Intent; 6 import android.net.Uri; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.view.KeyEvent; 10 import android.webkit.WebChromeClient; 11 import android.webkit.WebSettings; 12 import android.webkit.WebView; 13 import android.webkit.WebViewClient; 14 import android.widget.Toast; 15 16 public class MainActivity extends AppCompatActivity { 17 private WebView webView; 18 private ProgressDialog progressDialog; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 /* 如何使用WevbView: 24 1.將WebView加入到你的應用 25 2.使用WebView加載頁面 26 --要在WebView加載頁面,使用loadUrl() 27 --web資源:webView.loadUrl("http://www.baidu.com"); 28 (本地文件使用: webView.loadUrl("file:///android_asset/xxxx.html");) 29 --使頁面得到焦點: webView.requestFocus(); //不然有的輸入框不會有響應 30 3.獲取網絡訪問權限: 31 在它有效工做以前,你要保證你的應用能訪問網絡,要訪問網絡,須要在你的配置我就愛你中獲取INTERNET權限: 32 <uses-permission android:name="android.permission.INTERNET">*/ 33 /* Uri uri = Uri.parse("https://www.so.com/s?q=%E5%8D%9A%E5%AE%A2%E5%9B%AD&src=srp&fr=360chrome_newtab_hotkeyword&psid=ad5a7920e79bb91520389b43574a4bf4");//url爲你要連接的地址 34 Intent intent = new Intent(Intent.ACTION_VIEW,uri); 35 startActivity(intent);*/ 36 init(); 37 } 38 39 private void init() { 40 webView= (WebView) findViewById(R.id.webView); 41 // webView加載web資源 42 webView.loadUrl("http://news.qq.com/"); 43 // webView加載本地資源 44 // webView.loadUrl("file:///android_asset/example.html");/*examplele.html名字不能爲中文,不然會報錯,*/ 45 // 覆蓋webView默認經過第三方或者是系統瀏覽器打開的行爲,使得網頁能夠在WebView中打開, 46 webView.setWebViewClient(new WebViewClient(){ 47 @Override 48 public boolean shouldOverrideUrlLoading(WebView view, String url) { 49 // 返回值是true時控制網頁在WebView中打開,爲false調用系統瀏覽器或第三方瀏覽器打開, 50 view.loadUrl(url); 51 return true; 52 } 53 // WebViewClient()幫助WebView處理一些頁面控制和請求通知, 54 }); 55 // 使用Webview加載javascript頁面 56 WebSettings webSettings= webView.getSettings(); 57 webSettings.setJavaScriptEnabled(true); 58 // WebView加載頁面優先使用緩存加載 59 webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 60 // WebView不使用緩存 61 webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); 62 // 判斷頁面加載過程 63 webView.setWebChromeClient(new WebChromeClient(){ 64 @Override 65 public void onProgressChanged(WebView view, int newProgress) { 66 // newProgress是1-100之間整數 67 if(newProgress==100){ 68 // 網頁加載完畢,關閉ProgressDialog 69 closeDialog(); 70 }else{ 71 // 網頁正在加載,打開ProgressDialog 72 openDialog(newProgress); 73 } 74 } 75 76 private void openDialog(int newProgress) { 77 if(progressDialog==null){ 78 progressDialog=new ProgressDialog(MainActivity.this); 79 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 80 progressDialog.setTitle("正在加載"); 81 // progressDialog.setProgress(newProgress); 82 progressDialog.show(); 83 }else{ 84 progressDialog.setProgress(newProgress); 85 } 86 } 87 88 private void closeDialog() { 89 if(progressDialog!=null&&progressDialog.isShowing()){ 90 progressDialog.dismiss(); 91 progressDialog=null; 92 } 93 } 94 }); 95 } 96 // 改寫返回邏輯,讓返回鍵返回上一頁面,不是退出,前提是覆蓋webView默認經過第三方或者是系統瀏覽器打開的行爲,使得網頁能夠在WebView中打開, 97 @Override 98 public boolean onKeyDown(int keyCode, KeyEvent event) { 99 if(keyCode==KeyEvent.KEYCODE_BACK){ 100 // Toast.makeText(this,webView.getUrl(),Toast.LENGTH_SHORT).show();/*通過重定向技術進行返回*/ 101 if(webView.canGoBack()){ 102 webView.goBack(); 103 return true; 104 }else { 105 System.exit(0); 106 } 107 } 108 return super.onKeyDown(keyCode, event); 109 } 110 }

 

layout 
1
<?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="webdemo.example.administrator.webdemo.MainActivity"> 11 12 <WebView 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 android:id="@+id/webView"></WebView> 16 </RelativeLayout>
相關文章
相關標籤/搜索