1. 程式人生 > 實用技巧 >專屬空間七——學校官網(webview與js的互動)

專屬空間七——學校官網(webview與js的互動)

    現在是學習了一下webview與js的網頁互動。

  在專屬在空間中相當於直接內建了一個網頁;

  !!!前提是:新增網路許可權:<uses-permission android:name="android.permission.INTERNET"/>

  今天所做的主要運用了WebViewClient類,來處理各種通知&請求事件

  shouldOverrideUrlLoading()

  作用:開啟網頁時不呼叫系統瀏覽器, 而是在本WebView中顯示;在網頁上的所有載入都經過這個方法,這個函式我們可以做很多操作。

  WebChromeClient類:作用:輔助 WebView 處理 Javascript 的對話方塊,網站圖示,網站標題等等。

  具體使用了

  onProgressChanged()

   作用:獲得網頁的載入進度並顯示

  onReceivedTitle()

    作用:獲取web網頁中的標題

  在 Activity 銷燬( WebView )的時候,先讓 WebView 載入null內容,然後移除 WebView,再銷燬 WebView,最後置空。

  頁面是可以進行縮放的,如果鎖定的話,字型顯得太小了。所以沒有設定。

package com.example.td;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle; import android.view.KeyEvent; import android.view.ViewGroup; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView; import
androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { WebView mWebview; WebSettings mWebSettings; TextView beginLoading,endLoading,loading,mtitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebview = (WebView) findViewById(R.id.webView1); beginLoading = (TextView) findViewById(R.id.text_beginLoading); endLoading = (TextView) findViewById(R.id.text_endLoading); loading = (TextView) findViewById(R.id.text_Loading); mtitle = (TextView) findViewById(R.id.title); mWebSettings = mWebview.getSettings(); mWebview.loadUrl("http://tiedao.vatuu.com/index.html"); mWebSettings.setJavaScriptEnabled(true); //設定頁面支援js互動 mWebSettings.setUseWideViewPort(true); //將圖片調整到適合webview的大小 mWebSettings.setLoadWithOverviewMode(false); //縮放至螢幕的大小 mWebSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //設定webview的快取方式 mWebSettings.setAllowFileAccess(true); //設定可以訪問檔案 mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支援js開啟新視窗 mWebSettings.setLoadsImagesAutomatically(true); //支援自動載入圖片 mWebSettings.setDefaultTextEncodingName("utf-8"); //設定編碼格式 //設定不用系統瀏覽器開啟,直接顯示在當前Webview mWebview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); //設定WebChromeClient類 mWebview.setWebChromeClient(new WebChromeClient() { //獲取網站標題 @Override public void onReceivedTitle(WebView view, String title) { mtitle.setText(title); } //獲取載入進度 @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress < 100) { String progress = newProgress + "%"; loading.setText(progress); } else if (newProgress == 100) { String progress = newProgress + "%"; loading.setText(progress); } } }); //設定WebViewClient類 mWebview.setWebViewClient(new WebViewClient() { //設定載入前的函式 @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { beginLoading.setText("開始載入了"); } //設定結束載入函式 @Override public void onPageFinished(WebView view, String url) { endLoading.setText("結束載入了"); } }); } //點選返回上一頁面而不是退出瀏覽器 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && mWebview.canGoBack()) { mWebview.goBack(); return true; } return super.onKeyDown(keyCode, event); } //銷燬Webview @Override protected void onDestroy() { if (mWebview != null) { mWebview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null); mWebview.clearHistory(); ((ViewGroup) mWebview.getParent()).removeView(mWebview); mWebview.destroy(); mWebview = null; } super.onDestroy(); } }
<?xml version="1.0" encoding="utf-8"?>
<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"

    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context="com.example.td.MainActivity">


    <!-- 獲取網站的標題-->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""/>

    <!--開始載入提示-->
    <TextView
        android:id="@+id/text_beginLoading"
        android:layout_below="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""/>

    <!--獲取載入進度-->
    <TextView
        android:layout_below="@+id/text_beginLoading"
        android:id="@+id/text_Loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""/>

    <!--結束載入提示-->
    <TextView
        android:layout_below="@+id/text_Loading"
        android:id="@+id/text_endLoading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""/>

    <!--顯示網頁區域-->
    <WebView
        android:id="@+id/webView1"
        android:layout_below="@+id/text_endLoading"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp" />
</RelativeLayout>