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

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

  1. Android -- 自定义带进度条的按钮

    1. 实现了一个带进度条的按钮,完成后显示提示信息,并设置按钮为不可再次被点击

  2. 【Android】带进度条的WebView

    http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html

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

    finddreams:http://blog.csdn.net/finddreams/article/details/44172639 为了仿微信中加载网页时带进度条的WebView的实现,首先我们来 ...

  4. atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7

    atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...

  5. 带有进度条的WebView

    带有进度条的WebView 本篇继于WebView的使用 效果图 自定义一个带有进度条的WebView package com.kongqw.kbox.view; import android.con ...

  6. 【Web】前端文件上传,带进度条

    最近做项目发现,在文件上传的过程中,增加进度条,能大大改善用户体验.本例介绍带进度条的文件上传 环境搭建 参考:[Java]JavaWeb文件上传和下载. 原生ajax上传带进度条 <%@ pa ...

  7. Extjs 使用fileupload插件上传文件 带进度条显示

    一.首先我们看看官方给出的插件的解释: 一个文件上传表单项具有自定义的样式,并且可以控制按钮的文本和 像文本表单的空文本类似的其他特性. 它使用一个隐藏的文件输入元素,并在用户选择文件后 在form提 ...

  8. struts2:上传多个文件时实现带进度条、进度详细信息的示范

    上一篇文章讲了上传单个文件与上传多个文件(属性驱动)的例子.本例是上传多个文件(属性驱动),并且显示进度条.进度详细信息的示范. 在文件上传选择界面,允许用户增加.删除选择的文件,且只能上传指定类型的 ...

  9. wxpython StatuBar 带进度条的状态栏

    # -*- coding: utf- -*- import wx class customStatusBar(wx.StatusBar): def __init__(self, parent): wx ...

随机推荐

  1. c/c++:动态库 静态库 linux/windows 例子 (转)

    作者:吴秦出处:http://www.cnblogs.com/skynet/本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名吴秦(包含链接). C++静 ...

  2. 使用docker搭建nfs实现容器间共享文件

    首先介绍下今天的两个主角:nfs和docker nfs 是什么 NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TC ...

  3. JavaScript手札:《编写高质量JS代码的68个有效方法》(一)(1~5)

    编写高质量JS代码的68个有效方法(一) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...

  4. 【转载】ubuntu和debian环境下无法挂载vmware虚拟机共享目录的解决办法

    转载自:http://www.fengfly.com/plus/view-210022-1.html 第一步,安装VMware Tools 打开虚拟机ubuntu(debian原理一样)后,首先,点击 ...

  5. IT人的自我导向型学习:学习的4个层次

    谈起软件开发一定会想到用什么技术.采用什么框架,然而在盛行的敏捷之下,人的问题逐渐凸显出来.不少企业请人来培训敏捷开发技术,却发现并不能真正运用起来,其中一个主要原因就是大家还没有很好的学习能力.没有 ...

  6. [C#高级编程]基础知识摘要一

    核心C#: 值类型存储在堆栈中,而引用类型存储在托管堆上. object类型可以用于两个目的: 可以使用object引用绑定任何子类型的对象 object类型执行许多一般用途的基本方法,包括Equal ...

  7. [C#] 语法之Attribute

    在c#中,定义类的成员,可以定义Property称为属性.Attribute就称为特性. 在FCL中,有内置的Attribute.如: Condition[Attribute]:在什么条件可以调用.( ...

  8. 【原创】有关Silverlight控件DataGrid的绑定数据后单元格单独复制的功能实现分析

    前些日子,公司新需求需要对silverlight的datagrid进行局部任意单元格数据可复制,查阅了半天网络资料愣是没找到相关资料,开始还以为是silverlight的bug根部无法实现, 最后还是 ...

  9. [CLR via C#]26. 计算限制的异步操作

    一.CLR线程池基础 前面说过,创建和销毁线程是一个比较昂贵的操作,太多的线程也会浪费内存资源.由于操作系统必须调度可运行的线程并执行上下文切换,所以太多的线程还有损于性能.为了改善这个情况,CLR使 ...

  10. csharp: Speech

    Speech SDK 5.1https://www.microsoft.com/en-us/download/details.aspx?id=10121 detects mobile devices ...