標籤:
依據我自己的測試,發現有時候用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載入頁面空白問題