自定义带进度条的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自定义带进度条的圆形图片
前言:在项目听新闻的改版中需要实现环绕圆形新闻图片的进度条功能,作为技术预备工作我就去看了一些网上的相关的原理,做了一个自定义带进度条的圆形图片的demo,并将这个实现写成文章发布出来,谁需要了可以进 ...
- Android开发之带进度条的WebView
老套路先看效果: 直接上代码: 区分java和kotlin版本 Java版本: package com.example.progresswebview;import android.os.Bundle ...
- uni-app中自定义图表(canvas实现chart图表)开发篇(1)-圆环带进度条
经常开发中,会遇到各种各样图表,这时大家普遍会想到去找插件.uniapp中常用的有uchart.js和echart.js,这对图表要求不高的项目来说,是很便捷的.但有时会遇到一些定制图表,加上UI的美 ...
- C#winform 从ftp下载文件(带进度条)、获取文件(夹)的名字
直接用地址下载,引用进度条: /// <summary>/// 从ftp服务器下载文件的功能----带进度条/// </summary>/// <param name= ...
- 用MediaPlayer做个带进度条可后台的音乐播放器
传送到github看源码 下载apk试用 密码:rukn 前言 想在天朝听音乐还是有些难度的, 一般来说, 两三个app还是要的. 那何不尝试自己做个手机播放器, 听个爽呢?!今天就带大家先做个简单的 ...
- Android 自定义View,自定义属性--自定义圆形进度条(整理)
很多的时候,系统自带的View满足不了我们的功能需求,那么我们就需要自定义View来满足我们的需求 自定义View时要先继承View,添加类的构造方法,重写父类View的一些方法,例如onDraw,为 ...
- 自定义圆形进度条 自定义倒计时进度条
自定义圆形进度条 自定义倒计时进度条 版权声明:转载必须注明本文转自严振杰的博客: http://blog.csdn.net/yanzhenjie1003 此控件源码已开源到Github:https: ...
- 自定义扇形进度条、异步加载网络图片、Activity疯狂旋转动画
效果预览: 正文 1.自定义扇形进度条. 思考: (1)如何绘制扇形. (2)重写onDraw(Canvas canvas) 或 draw(Canvas canvas). (3)是否保留背景设置. ( ...
- 直接修改html代码视频进度,HTML5实现video标签的自定义播放进度条
现在随着html5的渐热,越来越多的web开发者都开始选择使用html5写出一些比较好的web应用. html代码: HTML5-Video-Player .videoPlayer{ border: ...
最新文章
- linux mpstat命令
- 原来音色是波峰到波谷时间序列
- linux select与多进程的结合
- java list翻转_浅谈Java数据结构中的常见问题
- 【避坑】初次接项目的血与泪,扎坑了老
- JVM调优:图解对象内存分配过程
- 360浏览器linux版本_360安全浏览器崩溃解决方案
- Spring Cloud简介–配置(第一部分)
- Linux目录架构详解
- win10清理_无需第三方,win10也可以实现自动清理垃圾
- Spring的beans标签下可以有其他标签
- java可以看懂php代码吗_PHP笔记——java程序员看懂PHP程序
- 班服的图片展示和投票的实现
- Junit框架使用(4)--JUnit常用断言及注解
- iPhone屏幕尺寸
- 什么是第三方Cookie?别等生意都丢了,才去了解
- 如何申请邮件安全证书(S/MIME)实现邮件加密和数字签名
- matplotlib简要画图
- 决胜未来,2019年前端开发十大战略性技术布局 1
- 单工通信、半双工通信和全双工通信之间有什么区别。
热门文章
- 六十二、Springboot中的配置文件和全注解
- keras从入门到放弃(二)多项回归
- python 学习 我推荐这本书,适合特别没有程序基础或者编程思维较差的人,
- 博士申请 | 上海财经大学语言智能实验室招收2022年秋季入学博士生
- 用户的购物历史都有用吗?看作者如何运用对比学习来筛选相关项
- 2021年最值得期待的数据智能赛事之一,有何解题妙招?
- Word Embedding Papers | 经典再读之fastText
- 预告 | CSIG图像图形学科前沿讲习班:图神经网络
- 总结 | 计算机视觉领域最常见几中损失函数
- debug assertion failed是什么意思?_MD5是是什么?为什么很多压缩文件上都有这个东西?...