自訂帶進度條的WebView , 增加擷取web標題和url 回掉,webviewweb

來源:互聯網
上載者:User

自訂帶進度條的WebView , 增加擷取web標題和url 回掉,webviewweb

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 );    }}

 


 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.