WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用。
webview有两个方法:setWebChromeClient 和 setWebClient
setWebClient:主要处理解析,渲染网页等浏览器做的事情
setWebChromeClient:辅助WebView处理Javascript的对话框,网站图标,网站title,加载进度等
WebViewClient就是帮助WebView处理各种通知、请求事件的。
webview
package com.example.webview;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private WebView webView=null;
private ProgressBar title=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置进度条
// requestWindowFeature(Window.FEATURE_PROGRESS);
//requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
webView=(WebView) findViewById(R.id.webView);
title=(ProgressBar) findViewById(R.id.progressBartitle);
/* 调用系统或第三方浏览器,打开uri
*
* Uri uri=Uri.parse("http://www.baidu.com");
Intent web=new Intent(Intent.ACTION_VIEW,uri);
startActivity(web);*/
//使用自己的webViewClient打开网页
//需要在manifest文件中添加Internet权限
webView.loadUrl("http://www.qq.com");
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});
//设置支持javascript
WebSettings webSetting=webView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//设置进度条的改变
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
super.onProgressChanged(view, newProgress);
if(newProgress==100){
title.setProgress(0);
title.setVisibility(0);
}else{
if(!title.isShown()){
title.setVisibility(1);
}
title.setProgress(newProgress);
}
}
});
//路径规则file:///android_asset/文件名注意冒号后是三个‘/’,不要为我为神魔,我也不知道,反正累死我了,才调试出来
//一定要注意不是android_assets而是android_asset
// webView.loadUrl("file:///android_asset/example.html");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_BACK){
if(webView.canGoBack()){
webView.goBack();
return true;
}else{
System.exit(0);
}
}
return super.onKeyDown(keyCode, event);
}
}
清单文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="com.example.webview.MainActivity" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true" />
<ProgressBar
android:id="@+id/progressBartitle"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="15dp"
android:visibility="invisible"
android:max="100"
/>
</RelativeLayout>