自定义带进度条的WebView , 增加获取web标题和url 回掉
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 );
}
}

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Android WebView 获取网页的标题
final TextView txtTitle = (TextView) findViewById(R.id.txtTitle); final WebView webView = (WebView)findViewById(R.id.btnWebView); WebChromeClient wvcc = new WebChromeClient() { @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); txtTitle.setText("ReceivedTitle:" +title); } }; // 设置setWebChromeClient对象 webView.setWebChromeClient(wvcc); // 创建WebViewClient对象 WebViewClient wvc = new WebViewClient() { @Override public boolean shouldOverrideUrlLoadin...
-
下一篇
去掉 Android工程中让人很不爽的“黄色警告”
一:问题 二:解决方法 (1)选择android工程,右键AndroidTools —> Clear Lint Markers 这种方式能够清除android工程里面的所有警告信息,但很遗憾,下一次打开eclipse的时候还会继续提示,非常不爽! (2)使用@SuppressLint标注忽略指定的警告 要使用该标注,需要引入annotations.jar,默认新建工程的时候都会有这个jar包。如下使用方式: 在类,方法,变量等前面加上标注 @SuppressLint("NewApi"),这个NewApi一般是具体的类,不用记,提示信息会自动加上。加上标注能够解决不再警告了,但代码貌似会变得冗余一些。总之没有两全齐美的方法,我的做法是让他继续警告,程序能够正常跑起来就行! 三:相关链接 http://www.it165.net/pro/html/201307/6563.html
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL数据库在高并发下的优化方案
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS关闭SELinux安全模块
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2全家桶,快速入门学习开发网站教程
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Docker使用Oracle官方镜像安装(12C,18C,19C)