首页 > 技术文章 > WebView的使用

633sylss 2016-05-06 09:36 原文

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>

 

推荐阅读