1. 程式人生 > >Android控制元件 WebView載入網頁

Android控制元件 WebView載入網頁

新增控制元件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView1" />
</LinearLayout>



方法二、Activity程式碼:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;

namespace App1
{
    [Activity(Label = "百姓市場", MainLauncher = true, Icon = "@drawable/icon1")]
    public class MainActivity : Activity
    {
        int count = 1;
        Android.Webkit.WebView webview;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            //隱藏標題欄(須放在SetContentView函式之前)  
            this.RequestWindowFeature(WindowFeatures.NoTitle);
            //設定全屏  
            this.Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            Android.Webkit.WebView webview2 = FindViewById<Android.Webkit.WebView>(Resource.Id.webView1);
            this.webview = webview2;
            webview2.LoadUrl("http://www.bxsc.net");
            //載入專案中本地資料夾Assets下的test.html檔案  
            // webview2.LoadUrl("file:///android_asset/test.html");  
            // webview2.LoadUrl("file:///android_asset/abc/test.html");  
            //啟用指令碼  
            webview2.Settings.JavaScriptEnabled = true;

            #region --縮放--  
            //設定支援縮放(前提是網頁自身支援縮放)  
            webview2.Settings.SetSupportZoom(true);
            webview2.Settings.BuiltInZoomControls = true;

            //支援任意比例縮放  
            webview2.Settings.UseWideViewPort = true;
            //顯示縮放控制元件(放大/縮小按鈕)  
            webview2.Settings.DisplayZoomControls = false;

            //自適應螢幕  
            //  webview2.Settings.SetLayoutAlgorithm(WebSettings.LayoutAlgorithm.SingleColumn);  
            //webview2.Settings.LoadWithOverviewMode = true;  
            #endregion

            //webview2.ClearCache(true);  
            //後退  
            //webview2.GoBack();  

            //設定Web檢視  
            webview2.SetWebViewClient(new ExtWebViewClient());

        }

        int sftc = 0;
        public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
        {
            //當按下後退鍵時,返回WebView上一頁面,而不是結束  
            if (keyCode == Keycode.Back && webview.CanGoBack())
            {
                webview.GoBack();
                return true;
            }
            else
            {
                if(sftc == 0)
                {
                    Toast.MakeText(this, "在按一次退出程式", ToastLength.Long).Show();
                    sftc++;
                    if(sftc > 1)
                    {
                        return base.OnKeyDown(keyCode, e);
                    }

                    System.Timers.Timer timer = new System.Timers.Timer();
                    timer.Enabled = true;
                    timer.Interval = 3000;//執行間隔時間,單位為毫秒  
                    timer.Start();
                    timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer1_Elapsed);

                    return false;
                }
                else
                {
                    return base.OnKeyDown(keyCode, e);
                }
            }
        }

        private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            sftc = 0;
        }

    }

    public class ExtWebViewClient : Android.Webkit.WebViewClient
    {
        /// <summary>  
        /// 響應超連結事件,通過改變Load載入方式,  
        /// 使在WebView控制元件中的連線在當前檢視中開啟,而不是新視窗中開啟  
        /// </summary>  
        /// <param name="view"></param>  
        /// <param name="url"></param>  
        /// <returns></returns>  
        public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            //預設  
            //return base.ShouldOverrideUrlLoading(view, url);  
            view.LoadUrl(url);
            return true;
        }
    }
}