1、自定义ProgressWebView
package com.app.android05;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
/**
* @author admin
* 带进度条的WebView
*/
public class ProgressWebView extends WebView {
private Context context ;
private ProgressBar progressbar ;
private OnWebCallBack onWebCallBack ; //回调
public ProgressWebView(Context context) {
this( context , null ) ;
}
public ProgressWebView(Context context, AttributeSet attrs) {
this( context , attrs , android.R.attr.webTextViewStyle ) ;
}
public ProgressWebView(Context context, AttributeSet attrs, int defStyle) {
super( context , attrs , defStyle ) ;
this.context = context ;
init() ;
setWebViewClient( new MyWebViewClient() ) ;
setWebChromeClient( new WebChromeClient() ) ;
}
/**
* 设置ProgressBar
*/
void init(){
progressbar = new ProgressBar( context , null , android.R.attr.progressBarStyleHorizontal);
progressbar.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, 20 , 0, 0 ));
addView( progressbar ) ;
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
progressbar.setVisibility(GONE);
} else {
progressbar.setVisibility( VISIBLE ) ;
progressbar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
if( onWebCallBack != null ){ //获取标题
onWebCallBack.getTitle( title ) ;
}
}
}
/**
* 不重写的话,会跳到手机浏览器中
* @author admin
*/
public class MyWebViewClient extends WebViewClient {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) { // Handle the
goBack() ;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if( onWebCallBack != null ){ //获得WebView的地址
onWebCallBack.getUrl( url ) ;
}
}
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
lp.x = l;
lp.y = t;
progressbar.setLayoutParams(lp);
super.onScrollChanged(l, t, oldl, oldt);
}
/**
* 设置WebView的回掉器
* @param onWebCallBack
*/
void setOnWebCallBack ( OnWebCallBack onWebCallBack ){
this.onWebCallBack = onWebCallBack ;
}
}
interface OnWebCallBack{
/**
* 获取标题
* @param title
*/
void getTitle( String title ) ;
/**
* 获得WebView的地址
* @param url
*/
void getUrl( String url ) ;
}
2、xml 引用
<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"
tools:context="com.app.android05.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title" />
<TextView
android:id="@+id/url"
android:layout_below="@id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="url" />
<com.app.android05.ProgressWebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/url" />
</RelativeLayout>
3、MainActivity 调用
package com.app.android05;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView title_tv ;
private TextView url_tv ;
private ProgressWebView webView ;
private String url = "http://dict.youdao.com/" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.activity_main ) ;
title_tv = (TextView) findViewById( R.id.tv ) ;
url_tv = (TextView) findViewById( R.id.url ) ;
webView = (ProgressWebView) findViewById( R.id.web ) ;
//设置webview的回掉函数,获得Url
webView.setOnWebCallBack( new OnWebCallBack() {
//获取标题
@Override
public void getTitle(String title) {
title_tv.setText( title ) ;
}
//获取当前web的URL地址
@Override
public void getUrl(String url) {
url_tv.setText( url );
}
});
webView.loadUrl( url );
}
}