自定义带进度条的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 );
}
}
自定义带进度条的WebView , 增加获取web标题和url 回掉的更多相关文章
- Android -- 自定义带进度条的按钮
1. 实现了一个带进度条的按钮,完成后显示提示信息,并设置按钮为不可再次被点击
- 【Android】带进度条的WebView
http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
- 仿微信中加载网页时带线行进度条的WebView的实现
finddreams:http://blog.csdn.net/finddreams/article/details/44172639 为了仿微信中加载网页时带进度条的WebView的实现,首先我们来 ...
- atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7
atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...
- 带有进度条的WebView
带有进度条的WebView 本篇继于WebView的使用 效果图 自定义一个带有进度条的WebView package com.kongqw.kbox.view; import android.con ...
- 【Web】前端文件上传,带进度条
最近做项目发现,在文件上传的过程中,增加进度条,能大大改善用户体验.本例介绍带进度条的文件上传 环境搭建 参考:[Java]JavaWeb文件上传和下载. 原生ajax上传带进度条 <%@ pa ...
- Extjs 使用fileupload插件上传文件 带进度条显示
一.首先我们看看官方给出的插件的解释: 一个文件上传表单项具有自定义的样式,并且可以控制按钮的文本和 像文本表单的空文本类似的其他特性. 它使用一个隐藏的文件输入元素,并在用户选择文件后 在form提 ...
- struts2:上传多个文件时实现带进度条、进度详细信息的示范
上一篇文章讲了上传单个文件与上传多个文件(属性驱动)的例子.本例是上传多个文件(属性驱动),并且显示进度条.进度详细信息的示范. 在文件上传选择界面,允许用户增加.删除选择的文件,且只能上传指定类型的 ...
- wxpython StatuBar 带进度条的状态栏
# -*- coding: utf- -*- import wx class customStatusBar(wx.StatusBar): def __init__(self, parent): wx ...
随机推荐
- HandlerMethodArgumentResolver数据绑定无效
因项目中action参数过多,并且是一些通用的查询参数,所以准备进行对参数统一封装为Map对象,然后由action中传入service中进行处理,查询spring的资料发现可以通过实现HandlerM ...
- LDPC编译码基本原理
LDPC编译码基本原理 学习笔记 V1.1 2015/02/18 LDPC编译码基本原理 概述 本文是个人针对LDPC的学习笔记,主要针对LDPC译码算法做了简要的总结.该版本主要致力 ...
- 如何拿到国内IT巨头的Offer
感觉写的很真实,分享一下.原文链接:http://jingyan.baidu.com/article/72ee561aa16d23e16138df3d.html 不久前,byvoid面阿里星计划的面试 ...
- C#语法糖之 cache操作类 asp.net
因为考虑到我下面我将写session cookies 等 操作类 ,与cache具有共性. 所以都统一继承了IHttpStorageObject abstract class 来保函数风格的统一 , ...
- XML to Entity
public static T GetEntityByXml<T>(string xml, string rootNode=null) where T : new() { if (stri ...
- mysql 在row模式下truncate 与 delete 二进制日志记录的差异
二进行日志的格式为row mysql> show variables like 'binlog_format'; +---------------+-------+ | Variable_nam ...
- [C#] 可空类型的实现原理
int? 是可为null的值类型.只比int多一个值就是null. 思考: 同样的内存空间,怎么实现的多一个值的?都是4字节,32位,int?靠什么存在一个null值的. 发现: 分析一下内存,看看如 ...
- 《构建之法》第8、9、10章的读后感和第一个sprint总结
第八章——主要介绍软件需求. 主要步骤:1.获取和引导需求.2.分析和定义需求.3.验证需求.4.在软件产品的生命周期中管理需求. 对软件需求的划分:1.对产品功能性的需求.2.对产品开发过程的需求. ...
- 内核移植和文件系统制作(2):linux内核最小系统和initramfs文件系统
linux内核最小系统,使用内核版本:https://www.kernel.org/pub/linux/kernel/v3.0/linux-3.8.1.tar.bz2 1,FL2440板子的基本硬件: ...
- Java中的继承
我们在以前的学习中,我们会了C#中的继承,今天我们来了解了解Java中的继承,其实都大同小异啦! 1.语法 修饰符 SubClass extends SuperClass(){ //类定义部分 } e ...