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

简介: 1、自定义ProgressWebView package com.app.android05; import android.content.Context; import android.
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 );
    }
}

 


 

 

相关文章
|
10月前
|
前端开发 JavaScript Java
Web.xml - Servlet与Filter的url-pattern
Web.xml - Servlet与Filter的url-pattern
83 8
|
Web App开发 前端开发 JavaScript
SAP UI5 应用开发教程之八十七 - 如何让 SAP UI5 Mock 服务器支持自定义 url 参数试读版
SAP UI5 应用开发教程之八十七 - 如何让 SAP UI5 Mock 服务器支持自定义 url 参数试读版
|
11月前
Thymeleaf内置对象、定义变量、URL参数及标签自定义属性
Thymeleaf内置对象、定义变量、URL参数及标签自定义属性
242 0
|
12月前
|
API Android开发 数据安全/隐私保护
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
772 0
|
2月前
|
数据安全/隐私保护 开发者 Ruby
【深度揭秘】Rails高手都不说的秘密:玩转URL映射,让你的Web应用瞬间高大上!
【8月更文挑战第31天】Rails中的路由机制负责将HTTP请求映射到应用内部逻辑。本文通过问答形式,结合示例代码详细解释了路由的作用、定义及使用方法。在`config/routes.rb`中定义的`resources :articles`会自动生成CRUD操作所需的标准RESTful路由。此外,还介绍了如何自定义非标准路由以及命名路由的生成与使用,帮助开发者更灵活地管理URL与应用逻辑间的映射关系,提升Rails应用的健壮性和可维护性。
28 0
|
26天前
|
安全 PHP 开发者
Web安全-URL跳转与钓鱼
Web安全-URL跳转与钓鱼
26 8
|
5月前
|
监控 数据库
第六十七章 使用 Web 服务监控 IRIS - 监控 Web 服务的 URL
第六十七章 使用 Web 服务监控 IRIS - 监控 Web 服务的 URL
41 0
|
5月前
|
Java 应用服务中间件
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
1063 1
|
5月前
钉钉接收事件订阅的url可以加自定义参数吗?
钉钉接收事件订阅的url可以加自定义参数吗?
83 0
|
11月前
|
定位技术 Android开发
[√]Android webview的url scheme
[√]Android webview的url scheme
513 0
下一篇
无影云桌面