自定义带进度条的WebView , 增加获取web标题和url 回掉

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 );
    }
}

 

 

 

时间: 2024-08-01 17:14:00

自定义带进度条的WebView , 增加获取web标题和url 回掉的相关文章

【Android】自定义带进度条的WebView,修复不弹出软键盘的BUG

记录下最近开发中研究的带进度条的WebView 自定义类吧. 其实网上有不少这样的帖子,但是都没有一个完整的好用的例子,最关键的是,用网上的例子后有一个很明显的bug,就是遇到输入框的话没法弹出软键盘.研究了好久总算搞定了.特此记录下. 直接上源代码,关于代码的解释,个人感觉注释中已经添加的足够清楚了. ProgressWebView import android.content.Context; import android.content.res.Resources; import andr

Android实现带进度条的WebView

如果不使用系统自带的TitleBar(即Activity被设置@android:style/Theme.NoTitleBar),那就需要自己来写进度条了,这里封装了一个自定义控件和加载网页的公共Activity,方便使用. 一.截图 二.自定义控件 /** * 带进度条的WebView * @author 农民伯伯 * @see http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html * */ @SuppressWarning

带进度条的webview

     如果不使用系统自带的TitleBar(即Activity被设置@android:style/Theme.NoTitleBar),那就需要自己来写进度条了,这里封装了一个自定义控件和加载网页的公共Activity,方便使用.   声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com      正文 一.截图     二.自定义控件   复制代码 /**  * 带进度条的WebVie

【Android】带进度条的WebView

前言  如果不使用系统自带的TitleBar(即Activity被设置@android:style/Theme.NoTitleBar),那就需要自己来写进度条了,这里封装了一个自定义控件和加载网页的公共Activity,方便使用.   声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com      正文 一.截图     二.自定义控件   /**  * 带进度条的WebView  *

Android 实现带进度条的WebView的实例

Android 实现带进度条的WebView的实例 1. WebView加载网页方法 //加载本地资源 loadUrl("file:///android_asset/example.html"); //加载网络资源 loadUrl("http://baidu.com"); 2. 带进度的Drawable文件view_progress_webview <?xml version="1.0" encoding="utf-8"

仿微信中加载网页时带线行进度条的WebView的实现

finddreams:http://blog.csdn.net/finddreams/article/details/44172639  为了仿微信中加载网页时带进度条的WebView的实现,首先我们来看一下微信中的效果是什么样的:  明确需求之后,我们来开始动手做,首先我们来自定义一个带进度条的WebView,名字为ProgressWebView: <code class="hljs java has-numbering" style="display: block;

Android中实现Webview顶部带进度条的方法_Android

写这篇文章,做份备忘,简单滴展示一个带进度条的Webview示例,进度条位于Webview上面. 示例图如下: 主Activity代码: 复制代码 代码如下: package com.droidyue.demo.webviewprogressbar; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.vi

Android中实现Webview顶部带进度条的方法

写这篇文章,做份备忘,简单滴展示一个带进度条的Webview示例,进度条位于Webview上面. 示例图如下: 主Activity代码: 复制代码 代码如下: package com.droidyue.demo.webviewprogressbar; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.vi

Adnroid打造通用的带进度条的WebView_Android

在Android开发中,经常需要加载显示网页,一般一个页面在打开后,在等待数据加载的过程中,都需要花一点时间,这个时候往往需要显示一个转动的进度条(ProgressBar),接下来封装了一个自定义控件和加载网页的公共Activity,方便使用. 一般的做法是在layout.xml中添加ProgressBar,但我们不这样做,主要是为了减少layout嵌套. 按照惯例我们先来看看最终的效果图: 在调用的时候很简单,就只需要传递一个url(加载网页的url)和title(显示标题)就可以了,如下所示