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. oracle rac理解和用途扩展

    Oracle RAC的优势在于利用多个节点(数据库实例)组成一个数据库,这样在保证了数据库高可用性的情况下更充分的利用了多个主机的性能,而且可以通过增加节点进行性能的扩展.实现Oracle RAC需要 ...

  2. js map

    // js通用方法 // map对象定义 function Map() { var struct = function(key, value) { this.key = key; this.value ...

  3. [转]c#截取指定长度的字符串

    /// <summary> /// 截取指定長度的字符串 /// </summary> /// <param name="s"></par ...

  4. 个人对joomla3.2x和joomla2.5X浅薄看法

    很久没有写joomla文章了,发现想写的东西还是挺多的,后面抽时间补回来,其实更多还是php的一些东西.joomla3.0以后系统改变挺大,后台都是用bootstrap作为主题,个人对这个无爱,因为他 ...

  5. 斜堆(二)之 C++的实现

    概要 上一章介绍了斜堆的基本概念,并通过C语言实现了斜堆.本章是斜堆的C++实现. 目录1. 斜堆的介绍2. 斜堆的基本操作3. 斜堆的C++实现(完整源码)4. 斜堆的C++测试程序 转载请注明出处 ...

  6. Install Redis on CentOS 6.4--转

    Install Redis on CentOS 6.4 source:http://thoughts.z-dev.org/2013/05/27/install-redis-on-centos-6-4/ ...

  7. ASP.NET MVC3 在_ViewStart設定Layout後用RenderAction的注意事項

    ASP.NET MVC3 在_ViewStart設定Layout後用RenderAction的注意事項 3/24 TW MVC第一次活動圓滿的結束了,雖然是RC,但也來了不少願意聽我們分享的好朋友. ...

  8. python BDD 框架之lettuce

    http://pythonhosted.org/lettuce/ 介绍                                                                 ...

  9. 十分钟搞懂什么是CGI

    原文:CGI Made Really Easy,在翻译的过程中,我增加了一些我在学习过程中找到的更合适的资料,和自己的一些理解.不能算是严格的翻译文章,应该算是我的看这篇文章的过程的随笔吧. CGI真 ...

  10. HTTP请求响应报文&&相关状态码&&GET_POST请求方法 总结

    HTTP请求报文: 一个HTTP请求报文由四个部分组成:请求行.请求头部.空行.请求数据 1.请求行   请求行由请求方法字段.URL字段和HTTP协议版本字段3个字段组成,它们用空格分隔.比如 GE ...