android開發中WebView的使用(附完整程式)

來源:互聯網
上載者:User

WebView是個好東西,作用相當於一個迷你的瀏覽器,採用Webkit核心,因此完美支援html,javascript,css等。有時候,我們完 全可以把UI甚至資料處理都交給WebView,配合PHP等服務端程式,這樣Android開發就變成了網頁開發,可以省很多精力。
下面是一個WebView的簡單例子,如果用把所有功能都交給服務端指令碼處理,這個程式已經很完整了,你只要寫好網頁,把URL填上,再編譯,就是一個新軟體。
程式功能介紹:開啟網頁同時顯示一個ProgressDialog,網頁載入完畢則隱藏,點擊頁面上連結時再次顯示ProgressDialog,載入完隱藏,可用返回鍵返回上一頁。

XML布局:

<?xml version="1.0" encoding="UTF-8"?><AbsoluteLayout android:orientation="vertical" android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent"  xmlns:android="http://schemas.android.com/apk/res/android">    <WebView android:id="@+id/wv"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_x="0.0dip"    android:layout_y="0.0dip"    android:layout_weight="1.0" /></AbsoluteLayout>

JAVA代碼:

package com.pocketdigi.webview; import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.KeyEvent;import android.webkit.WebChromeClient;import android.webkit.WebView;import android.webkit.WebViewClient; public class main extends Activity {    /** Called when the activity is first created. */WebView wv;ProgressDialog pd;Handler handler;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        init();//執行初始化函數        loadurl(wv,"http://www.pocketdigi.com");        handler=new Handler(){        public void handleMessage(Message msg)        {//定義一個Handler,用於處理下載線程與UI間通訊          if (!Thread.currentThread().isInterrupted())          {            switch (msg.what)            {            case 0:            pd.show();//顯示進度對話方塊                    break;            case 1:            pd.hide();//隱藏進度對話方塊,不可使用dismiss()、cancel(),否則再次調用show()時,顯示的對話方塊小圓圈不會動。            break;            }          }          super.handleMessage(msg);        }        };    }    public void init(){//初始化    wv=(WebView)findViewById(R.id.wv);        wv.getSettings().setJavaScriptEnabled(true);//可用JS        wv.setScrollBarStyle(0);//捲軸風格,為0就是不給捲軸留空間,捲軸覆蓋在網頁上        wv.setWebViewClient(new WebViewClient(){               public boolean shouldOverrideUrlLoading(final WebView view, final String url) {            loadurl(view,url);//載入網頁                return true;               }//重寫點擊動作,用webview載入         });        wv.setWebChromeClient(new WebChromeClient(){        public void onProgressChanged(WebView view,int progress){//載入進度改變而觸發              if(progress==100){            handler.sendEmptyMessage(1);//如果全部載入,隱藏進度對話方塊            }                   super.onProgressChanged(view, progress);               }           });     pd=new ProgressDialog(main.this);        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);        pd.setMessage("資料載入中,請稍候!");    }    public boolean onKeyDown(int keyCode, KeyEvent event) {//捕捉返回鍵        if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {               wv.goBack();               return true;           }else if(keyCode == KeyEvent.KEYCODE_BACK){        ConfirmExit();//按了返回鍵,但已經不能返回,則執行退出確認        return true;         }           return super.onKeyDown(keyCode, event);       }    public void ConfirmExit(){//退出確認    AlertDialog.Builder ad=new AlertDialog.Builder(main.this);    ad.setTitle("退出");    ad.setMessage("是否退出軟體?");    ad.setPositiveButton("是", new DialogInterface.OnClickListener() {//退出按鈕@Overridepublic void onClick(DialogInterface dialog, int i) {// TODO Auto-generated method stubmain.this.finish();//關閉activity }});    ad.setNegativeButton("否",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int i) {//不退出不用執行任何操作}});    ad.show();//顯示對話方塊    }    public void loadurl(final WebView view,final String url){    new Thread(){        public void run(){        handler.sendEmptyMessage(0);        view.loadUrl(url);//載入網頁        }        }.start();    }  }
相關文章

聯繫我們

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