iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 6
0
Mobile Development

Android的30天學習歷程系列 第 6

第六篇:進階元件使用(webview)

  • 分享至 

  • twitterImage
  •  

前言:我們在寫專案時有時會需要用到網路上的一些頁面,而使用 WebView 就可以輕鬆連上網頁。

第一步:添加網路許可

<uses-permission android:name="android.permission.INTERNET" />

第二步:在布局文件xml檔裡添加 WebView 元件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.6"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>

第三步:呼叫元件以及元件基礎設定

webview 的4種存取方式:

LOAD_DEFAULT: (默認)根據cache-control決定是否從網絡上取數據。
LOAD_CACHE_ONLY: 不使用網絡,只讀取本地緩存數據
LOAD_NO_CACHE: 不使用緩存,只從網絡獲取數據.
LOAD_CACHE_ELSE_NETWORK,只要本地有,無論是否過期,或者no-cache,都使用緩存中的數據。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        webView = findViewById(R.id.webview);
        webView.loadUrl("https://www.google.com/?hl=zh_CN");//你想要進入的網址
        webView.addJavascriptInterface(this,"android");
        //添加js監聽 這樣html就能調用客戶端

        webView.setWebViewClient(webViewClient);
        //(若沒有這行當跳轉頁面時將會跑到手機的google app上)
        
        WebSettings webSettings=webView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setSupportZoom(true); //支持屏幕缩放
        webSettings.setBuiltInZoomControls(true);//不显示webview缩放按钮
    }

第四步:創建帮助WebView處理之物件

private WebViewClient webViewClient=new WebViewClient(){};

第五步:偵測是否有上一頁

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i("ansen","是否有上一个页面:"+webView.canGoBack());
        if (webView.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK){//點擊返回按鈕的時候判斷有沒有上一頁
            webView.goBack(); // goBack()表示回webView的上一頁
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

以下為完整程式碼

package com.example.webview;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private WebView webView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.webview);
        webView.loadUrl("https://www.google.com/?hl=zh_CN");//加载url
        webView.setWebViewClient(webViewClient);

        WebSettings webSettings = webView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        //支持屏幕缩放
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        //不显示webview缩放按钮
//        webSettings.setDisplayZoomControls(false);
    }

    //WebViewClient主要帮助WebView处理各种通知、请求事件
    private WebViewClient webViewClient = new WebViewClient() {

    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i("ansen", "是否有上一个页面:" + webView.canGoBack());
        if (webView.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK) {//点击返回按钮的时候判断有没有上一页
            webView.goBack(); // goBack()表示返回webView的上一页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}


上一篇
第五篇:介紹基礎元件
下一篇
第七篇:webview + 讀取條
系列文
Android的30天學習歷程30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言