自定义带进度条的WebView , 增加获取web标题和url 回掉
生活随笔
收集整理的這篇文章主要介紹了
自定义带进度条的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 {@Overridepublic void onProgressChanged(WebView view, int newProgress) {if (newProgress == 100) {progressbar.setVisibility(GONE);} else {progressbar.setVisibility( VISIBLE ) ; progressbar.setProgress(newProgress);}super.onProgressChanged(view, newProgress);}@Overridepublic void onReceivedTitle(WebView view, String title) {super.onReceivedTitle(view, title);if( onWebCallBack != null ){ //獲取標題
onWebCallBack.getTitle( title ) ;}}}/*** 不重寫的話,會跳到手機瀏覽器中* @author admin*/public class MyWebViewClient extends WebViewClient {@Overridepublic void onReceivedError(WebView view, int errorCode,String description, String failingUrl) { // Handle the
goBack() ;}@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;}@Overridepublic void onPageFinished(WebView view, String url) {super.onPageFinished(view, url);}@Overridepublic void onPageStarted(WebView view, String url, Bitmap favicon) {if( onWebCallBack != null ){ //獲得WebView的地址
onWebCallBack.getUrl( url ) ;}}}@Overrideprotected 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" ><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="title" /><TextViewandroid:id="@+id/url"android:layout_below="@id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="url" /><com.app.android05.ProgressWebViewandroid:id="@+id/web"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@id/url" /></RelativeLayout>
3、MainActivity 調用
?
?
?
總結
以上是生活随笔為你收集整理的自定义带进度条的WebView , 增加获取web标题和url 回掉的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android v4、v7、v13 的区
- 下一篇: IOS 开发一些常用的地址