能否提供一個Android中WebView實現的具體示例?


在學習WebView實現之前,我們應該瞭解什麼是WebView。WebView是View的擴充套件,用於顯示HTML內容或網頁。

WebView提供以下方法:

  • clearHistory() − 用於清除WebView歷史記錄

  • destroy() − 用於銷燬WebView的內部狀態。

  • getUrl() − 用於返回當前WebView的URL。

  • getTitle() − 用於返回當前WebView的標題。

  • canGoBack() − 指示當前WebView是否有返回歷史記錄。

使用WebView會在預設的Android瀏覽器中開啟WebView內容。如果想在應用程式內部開啟,則應使用ShouldOverrideUrlLoading,如下所示。

private class MyWebViewClient extends WebViewClient {
   @Override
   public boolean shouldOverrideUrlLoading(WebView webView, String url) {
      return false;
   }
}

此示例演示如何在Android中實現WebView。

步驟1 − 在Android Studio中建立一個新專案,依次點選File ⇒ New Project,並填寫所有必需的詳細資訊以建立一個新專案。

步驟2 − 將以下程式碼新增到res/layout/activity_main.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
   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:layout_width = "match_parent"
      android:layout_height = "match_parent"
      android:id = "@+id/webView" />
</android.support.constraint.ConstraintLayout>

步驟3 − 將以下程式碼新增到src/MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {
   private WebView simpleWebView;
   private ProgressBar loadProgress;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      simpleWebView=findViewById(R.id.webView);
      simpleWebView.setWebViewClient(new WebViewClient());
      simpleWebView.getSettings().setLoadsImagesAutomatically(true);
      simpleWebView.getSettings().setJavaScriptEnabled(true);
      simpleWebView.setScrollBarStyle(View.VISIBLE);
      simpleWebView.getSettings().setBuiltInZoomControls(true);
      simpleWebView.getSettings().setSupportZoom(true);
      simpleWebView.getSettings().setLoadWithOverviewMode(true);
      simpleWebView.getSettings().setUseWideViewPort(true);
      simpleWebView.getSettings().setAllowContentAccess(true);
      simpleWebView.loadUrl("https://tutorialspoint.tw/");
   }
   @Override
   public void onBackPressed() {
      if (simpleWebView.canGoBack()) {
         simpleWebView.goBack();
      } else {
         super.onBackPressed();
      }
   }
}

在上面的程式碼中,您可以使用您自己的網站地址替換loadUrl();中的地址。

步驟4 − 將以下程式碼新增到AndroidManifest.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
   package = "com.example.andy.myapplication">
<uses-permission android:name = "android.permission.INTERNET"/>
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:roundIcon = "@mipmap/ic_launcher_round"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity android:name = ".MainActivity">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

在上面的程式碼中,我們添加了internet許可權,因為我們正在呼叫網際網路上的網站。

步驟5 − 將以下程式碼新增到res/values/string.xml。

<resources>
   <string name = "app_name">My Application</string>
   <string name = "erroopsproblem">Something error</string>
</resources>

讓我們嘗試執行您的應用程式。我假設您已將實際的Android移動裝置連線到計算機。要在Android Studio中執行應用程式,請開啟專案中的一個Activity檔案,然後點選工具欄中的執行 播放圖示 圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕。

Tutorials Point Html

現在,當您點選某些內容時,例如點選上面顯示的HTML圖示,將得到如下結果。

HTML Tutorial

點選此處下載專案程式碼

更新於:2019年7月30日

218次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.