(三) Android Webview 深入 (中)

來源:互聯網
上載者:User

標籤:webview   progressdialog   alertdialog   結合   

  建立在對webView控制項有了一定的認識和瞭解之後,我們就可以繼續研究一下這個控制項了,這篇文章主要介紹一下如何使webView與ProgressDialog結合。

WebView 組件支援直接載入網頁,可以將其視為一個瀏覽器,要實現該功能,具體步驟如下:

1.建立xml布局檔案,如下:

<span style="font-size:18px;"><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" >    <WebView        android:id="@+id/webView"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout></span>
2.在主Activity中對控制項進行初始化和設定相應的參數

<span style="font-size:18px;">package com.happy.wbpd;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.KeyEvent;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.ProgressBar;import android.widget.Toast;public class MainActivity extends Activity {private WebView webView;private AlertDialog alertDialog;private ProgressDialog progressBar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initWebView();}/** * 初始化webView */private void initWebView() {webView = (WebView) this.findViewById(R.id.webView);progressBar = ProgressDialog.show(MainActivity.this, null,"正在進入網頁,請稍後...");webView.getSettings().setJavaScriptEnabled(true);webView.loadUrl("http://www.baidu.com/");alertDialog = new AlertDialog.Builder(MainActivity.this).create();webView.setWebViewClient(new WebViewClient() {@Overridepublic void onPageFinished(WebView view, String url) {if (progressBar.isShowing()) {progressBar.dismiss();}}@Overridepublic void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {Toast.makeText(MainActivity.this, "網頁載入出錯!", Toast.LENGTH_LONG);alertDialog.setTitle("ERROR");alertDialog.setMessage(description);alertDialog.setButton("OK",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {}});alertDialog.show();}@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;}});}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {webView.goBack();return true;}return super.onKeyDown(keyCode, event);}}</span>
3.在需要在AndroidManifest.xml 中添加訪問互連網的許可權,否則不能顯示:
<uses-permission android:name="android.permission.INTERNET"/>


該邏輯比較簡單,外加今天比較忙,所以就大體介紹一下,下篇文章會說一下webView與javaScript的互相調用以及listView加入webView實現下拉重新整理。

(三) Android 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.