日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义带进度条的WebView , 增加获取web标题和url 回掉

發布時間:2024/9/30 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义带进度条的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 調用

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/" ; @Overrideprotected 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的回掉函數,獲得UrlwebView.setOnWebCallBack( new OnWebCallBack() {//獲取標題 @Overridepublic void getTitle(String title) {title_tv.setText( title ) ;}//獲取當前web的URL地址 @Overridepublic void getUrl(String url) {url_tv.setText( url );}});webView.loadUrl( url );} }

?


?

?

總結

以上是生活随笔為你收集整理的自定义带进度条的WebView , 增加获取web标题和url 回掉的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。