1. 程式人生 > >Android WebView的簡單使用,實現標題、載入進度提示

Android WebView的簡單使用,實現標題、載入進度提示

1.配置XML

<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity"
> <RelativeLayout android:layout_width="match_parent" android:layout_height="56dp"> <TextView android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:text="標題" android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content"
/> <com.daimajia.numberprogressbar.NumberProgressBarandroid:layout_alignParentBottom="true" app:progress_unreached_color="#ffffff" app:progress_reached_color="#4CAF50" app:progress_text_color="#4CAF50" android:id="@+id/pb_progress" android:layout_width="wrap_content" android:layout_height=
"wrap_content" /> <WebView android:id="@+id/activity_main_webview" android:layout_width="match_parent" android:layout_height="match_parent" />

其中用到第三方工具NumberProgressBar,

compile 'com.daimajia.numberprogressbar:library:[email protected]'

2.主要實現程式碼

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private WebView mWebView;
    private TextView mTv_title;
    private NumberProgressBar mPb_progress;@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mTv_title = (TextView) findViewById(R.id.tv_title);
mPb_progress = (NumberProgressBar) findViewById(R.id.pb_progress);
mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
Log.i(TAG,"newProgress="+newProgress);
mPb_progress.setProgress(newProgress);
                if (newProgress == 100){
                    mPb_progress.setVisibility(View.GONE);
}else {
                    mPb_progress.setVisibility(View.VISIBLE);
}

            }

            @Override
public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
Log.i(TAG,"title="+title);
mTv_title.setText(title);
}
            // 處理Alert事件
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Log.i(TAG,"onJsAlert:=url"+url);
Log.i(TAG,"onJsAlert:=message"+message);
                return super.onJsAlert(view, url, message, result);
}
            // 處理Confirm事件
@Override
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                Log.i(TAG,"onJsConfirm:=url"+url);
Log.i(TAG,"onJsConfirm:=message"+message);
                return super.onJsConfirm(view, url, message, result);
}
            // 處理提示事件
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
                Log.i(TAG,"onJsPrompt:=url"+url);
Log.i(TAG,"onJsPrompt:=message"+message);
                return super.onJsPrompt(view, url, message, defaultValue, result);
}
        });
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient(){
            @Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
Log.i(TAG,"failingUrl="+failingUrl);
Log.i(TAG,"description="+description);
}

            @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i(TAG,"shouldOverrideUrlLoading:url="+url);
//防止載入錯誤url
if (url != null && url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("https://")) {
                    return super.shouldOverrideUrlLoading(view, url);
} else{

                }
                return true;
}
        });
mWebView.setDownloadListener(new DownloadListener() {
            @Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                Log.i(TAG,"onDownloadStart:url="+url);
//呼叫本機應用下載
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}

        });
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://www.taobao.com/");
} // Prevent the back-button from closing the app@Overridepublic void onBackPressed() { if(mWebView.canGoBack()) { mWebView.goBack();} else { super.onBackPressed();} }

相關推薦

Android WebView簡單使用實現標題載入進度提示

1.配置XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" andro

Android WebView載入網頁實現前進後退重新整理超連結

你也可以檢視我的其他同類文章,也會讓你有一定的收貨! 先在要顯示網頁的佈局檔案中宣告WebView 在Activity中例項化WebView 呼叫WebView的loadUrl( )方法,設定WevView要顯示的網頁 為了讓WebView能夠響應超連結

Android一個簡單的警告框標題圖標按鈕的代碼

cli button bsp con creat raw new alert roi 工作之余,將內容過程比較常用的內容做個珍藏,下面內容是關於Android一個簡單的警告框,帶標題、圖標、按鈕的內容,應該是對大夥有些用。 AlertDialog alertDialog =

手機Android音視訊採集與直播推送實現單兵移動監控類應用

      最新手機採集推送直播監控以及EasyDarwin開源流媒體平臺的版本及程式碼:恰逢2014 Google I/O大會,不難看出安卓在Google的推進以及本身的開放性作用下,已經快延生到生活

Android Studio上使用GSON+VOLLEY秒處理網路資料成集合。感受框架的力量。搭配RecyclerView和SwipeRefreshLayout實現底端載入更多下拉重新整理。

【致謝,引用,宣告,前言】 關於GSON和VOLLEY,我百度了很多資料,個人感覺有兩篇部落格介紹的特別好,附上鍊接咯:   GSON: http://blog.csdn.net/lk_blog/article/details/7685169  VOLLEY:http://

十一接口(接口的概念實現繼承實現抽象類與抽象方法(抽象類抽象方法概念使用)

輸出 重寫 關鍵字 new clas main ride ring strac 接口 接口是一種用來定義程序的協議,它描述可屬於任何類和結構的一組相關行為。 接口可由方法、屬性、事件和索引器這四種成員類型的任何組合構成,但不能包含字段。 接口通過類繼承來實現,一個類雖然只能

python操作資料庫實現使用者名稱密碼登入資料庫首次登入自行設定密碼並返回工資表明細。

python操作資料庫,實現使用者名稱、密碼登入資料庫,首次登入自行設定密碼,並返回工資表明細。 1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 # 匯入依賴包 5 import psycopg2 6 7 print("營

android studio中ToolBar實現標題

首先注意將values中的style類中改成NoActionBar <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light**.NoActionBar**

C#利用HttpWebRequestHttpWebResponse呼叫12306介面實現登入查票買票。

【免責申明】本文只為學習使用,若有用作商業、其他行為,與本人無關。 使用工具 - UI bootstrap - 後臺C# - 外掛 datetimepicker.js,select.js UI介面效果預覽 UI介面原始碼 <!DOCTYPE html&

SpringBoot整合Elasticsearch 進階實現[中文拼音繁簡體轉換]高階搜尋

Elasticsearch 分詞 分詞分為讀時分詞和寫時分詞。 讀時分詞發生在使用者查詢時,ES 會即時地對使用者輸入的關鍵詞進行分詞,分詞結果只存在記憶體中,當查詢結束時,分詞結果也會隨即消失。而寫時分詞發生在文件寫入時,ES 會對文件進行分詞後,將結果存入倒排索引,該部分最終會以

【SRH】------node連接mongodb實現查功能

get 準備工作 升序 toarray update move demo ole 完成 node連接mongodb 準備工作 1.在項目根目錄下 初始化倉庫,形成node包 npm init -y 2.下載mongodb包 cnpm inst

【SRH】------node連線mongodb實現查功能

node連線mongodb 準備工作 1.在專案根目錄下   初始化倉庫,形成node包 npm init -y   2.下載mongodb包

Android外掛化完美實現程式碼資源載入及原理講解 附可執行demo

*本篇文章已授權微信公眾號 guolin_blog (郭霖)獨家釋出 。 我們通過前4篇的分解,分別將外掛化設計到的知識點全部梳理了一遍,如果沒有看過的,建議先看前面4篇 6. 外掛化資源的使用及動態載入 附demo 好了上面介紹了之

VS2008下VLC播放器實現播放暫停停止快進截圖進度條顯示進度條控制功能

可以直接使用的原始碼:http://download.csdn.net/detail/dafenqie/9792806 1、首先建立一個基於對話方塊的應用程式; 2、建立完成後,把VLC的標頭檔案目錄vlc拷貝到工程目錄下,拷貝libvlc.dll、libvlccore.d

mnist LSTM 訓練測試模型儲存載入和識別

MNIST 字元資料庫每個字元(0-9) 對應一張28x28的一通道圖片,可以將圖片的每一列(行)當作特徵,所有行(列)當做一個序列。那麼可以通過輸入大小為28,時間長度為28的RNN(lstm)對字元建模。對於同一個字元,比如0,其行與行之間的動態變化可以

android webview中如何實現html與手機本地資料的互動

在webview開發中,往往不止簡單的載入一個頁面顯示那麼簡單,常常有將手機本地的資料放到html上顯示和將html上操作的資料儲存到手機本地的情況,那麼如何實現這種資料互動呢: 一:webvie部份: final String json="{title

android webview旋轉螢幕導致頁面重新載入問題

1. 在create時候加個狀態判斷protected void onCreate(Bundle savedInstanceState){ ... if (savedInstanceState ==

微服務框架一鍵打包實現開發部署環境分離

場景說明本專案中微服務專案共30+個工程包,開發環境開發人員只需啟動一個web工程、service工程,而部署時如上圖所示需要增加eureka、zuul等配置,如果將每個包進行單獨修改配置檔案,修改過多容易出錯,這時最好能夠進行一次修改,之後能夠一鍵打包。打包環境工程是基於m

Android自定義相機實現拍照預覽顯示上傳

自定義相機拍照並存放到本地,可以預覽,用okHttp上傳到伺服器 用法 1.點選登入進入到拍照頁面 2.拍照後進入到上傳介面,需要在Constant中修改BASE_URL為自己伺服器圖片上傳地

利用Thrift實現javaphp資料通訊

Thrift是一個軟體框架(遠端過程呼叫框架),用來進行可擴充套件且跨語言的服務的開發,封裝了資料傳輸格式(二進位制、json)和網路通訊的服務框架,提供多語言(C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScrip