自定义带进度条的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 );
    }
}

 


 

 

相关文章
|
11月前
Thymeleaf内置对象、定义变量、URL参数及标签自定义属性
Thymeleaf内置对象、定义变量、URL参数及标签自定义属性
254 0
|
2月前
|
安全 前端开发 API
【Azure 应用服务】Azure Web App 服务默认支持一些 Weak TLS Ciphers Suite,是否有办法自定义修改呢?
【Azure 应用服务】Azure Web App 服务默认支持一些 Weak TLS Ciphers Suite,是否有办法自定义修改呢?
|
2月前
|
数据安全/隐私保护 开发者 Ruby
【深度揭秘】Rails高手都不说的秘密:玩转URL映射,让你的Web应用瞬间高大上!
【8月更文挑战第31天】Rails中的路由机制负责将HTTP请求映射到应用内部逻辑。本文通过问答形式,结合示例代码详细解释了路由的作用、定义及使用方法。在`config/routes.rb`中定义的`resources :articles`会自动生成CRUD操作所需的标准RESTful路由。此外,还介绍了如何自定义非标准路由以及命名路由的生成与使用,帮助开发者更灵活地管理URL与应用逻辑间的映射关系,提升Rails应用的健壮性和可维护性。
29 0
|
5天前
|
监控 安全 Apache
构建安全的URL重定向策略:确保从Web到App平滑过渡的最佳实践
【10月更文挑战第2天】URL重定向是Web开发中常见的操作,它允许服务器根据请求的URL将用户重定向到另一个URL。然而,如果重定向过程没有得到妥善处理,可能会导致安全漏洞,如开放重定向攻击。因此,确保重定向过程的安全性至关重要。
16 0
|
2月前
|
API 开发者 Python
"FastAPI路由大揭秘!轻松玩转URL映射,让你的Web应用路由设计既RESTful又灵活多变,秒杀传统框架的秘籍在这里!"
【8月更文挑战第31天】在Web开发中,路由是连接用户请求与后端逻辑的关键。FastAPI作为现代Python Web框架的佼佼者,以其简洁的API设计和高性能,提供了高度灵活的路由系统。本文通过开发一个博客系统的案例,详细介绍了FastAPI中路由的实现方法,包括基础路由定义、参数类型验证及路由分组与嵌套等,展示了如何轻松构建RESTful风格的URL映射,提升应用的可维护性和扩展性。
58 2
|
2月前
|
开发者 Java UED
大文件传输不再头疼:揭秘Struts 2如何轻松应对文件上传与下载难题!
【8月更文挑战第31天】在Web应用开发中,文件上传与下载至关重要。Struts 2作为主流Java EE框架,凭借Commons FileUpload及文件上传拦截器简化了相关操作。本文探讨Struts 2在文件传输上的优势,通过具体配置与代码示例,展示如何设置最大文件大小、使用`fileUpload`拦截器以及实现文件上传与下载功能。对于大文件传输,Struts 2不仅能够轻松应对,还支持上传进度显示,有效提升了用户体验。总体而言,Struts 2为文件传输提供了高效便捷的解决方案,助力开发者构建稳定可靠的Web应用。然而,在处理大文件时需兼顾网络带宽与服务器性能,确保传输顺畅。
48 0
|
2月前
|
JavaScript PHP 开发者
PHP中的异常处理与自定义错误处理器构建高效Web应用:Node.js与Express框架实战指南
【8月更文挑战第27天】在PHP编程世界中,异常处理和错误管理是代码健壮性的关键。本文将深入探讨PHP的异常处理机制,并指导你如何创建自定义错误处理器,以便优雅地管理运行时错误。我们将一起学习如何使用try-catch块捕获异常,以及如何通过set_error_handler函数定制错误响应。准备好让你的代码变得更加可靠,同时提供更友好的错误信息给最终用户。
|
4月前
|
存储 移动开发 前端开发
Web网页制作-知识点(1)——HTML5介绍、HTML5的DOCTYPE声明、HTML基本骨架、标题标签、段落 换行、水平线图片图片路径、超链接
Web网页制作-知识点(1)——HTML5介绍、HTML5的DOCTYPE声明、HTML基本骨架、标题标签、段落 换行、水平线图片图片路径、超链接
56 0
|
5月前
|
监控 数据库
第六十七章 使用 Web 服务监控 IRIS - 监控 Web 服务的 URL
第六十七章 使用 Web 服务监控 IRIS - 监控 Web 服务的 URL
44 0
|
5月前
|
JavaScript 前端开发 架构师
Web Components:自定义元素与Shadow DOM的实践
Web Components是用于创建可重用自定义HTML元素的技术集合,包括Custom Elements、Shadow DOM、HTML Templates和Slots。通过Custom Elements定义新元素,利用Shadow DOM封装私有样式,&lt;slot&gt;元素允许插入内容。自定义元素支持事件处理和属性观察,可复用且样式隔离。它们遵循Web标准,兼容各前端框架,注重性能优化,如懒加载和Shadow DOM优化。
50 0