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


依據我自己的測試,發現有時候用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> 

效果圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM