安卓開發_關於WebView載入頁面空白問題

來源:互聯網
上載者:User

標籤:

依據我自己的測試,發現有時候用APP開啟網頁的時候,有的網頁載入成功之前需要很久,有的一下就出來了(比如百度)

當載入時間過長的情況下,這時候顯示的是空白介面,其實不是代碼問題,只是要開啟的這個網頁太大了。

那麼為了提高使用者體驗,我們就得想辦法在這個空白介面等待的情況下加點東西。

 

首先,想到的就是提示框

具體操作呢

package com.example.qunxiong;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.util.Log;import android.view.KeyEvent;import android.view.Window;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Toast;public class Web_shijianjinbi extends Activity {    private WebView webview;    private static final String TAG = "Web_shijianjinbi"; //類名    private ProgressDialog progressBar;       /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         requestWindowFeature(Window.FEATURE_NO_TITLE);         setContentView(R.layout.web_show);  //對應的layout         this.webview = (WebView)findViewById(R.id.webview);//這裡是layout中WebView控制項的Id         WebSettings settings = webview.getSettings();        settings.setJavaScriptEnabled(true);        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);         final AlertDialog alertDialog = new AlertDialog.Builder(this).create();         progressBar = ProgressDialog.show(Web_shijianjinbi.this, "這裡是提示框的標題", "這裡是提示框的內容");         webview.setWebViewClient(new WebViewClient() {            public boolean shouldOverrideUrlLoading(WebView view, String url) {                Log.i(TAG, "Processing webview url click...");                view.loadUrl(url);                return true;            }             public void onPageFinished(WebView view, String url) {                Log.i(TAG, "Finished loading URL: " +url);                if (progressBar.isShowing()) {                    progressBar.dismiss();                }            }             public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {                Log.e(TAG, "Error: " + description);                Toast.makeText(Web_shijianjinbi.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();                alertDialog.setTitle("Error");                alertDialog.setMessage(description);                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int which) {                        return;                    }                });                alertDialog.show();            }        });        //這裡是要開啟的頁面
    webview.loadUrl("http://www.baidu.com"); }}

下面是布局檔案 很簡單

 1 <?xml version="1.0" encoding="utf-8"?>  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  3     android:orientation="vertical"  4     android:layout_width="fill_parent"  5     android:layout_height="fill_parent"  6     >     7     <WebView    8         android:id="@+id/webview"  9         android:layout_width="fill_parent" 10         android:layout_height="fill_parent" 11         /> 12 </LinearLayout> 

 

安卓開發_關於WebView載入頁面空白問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.