WebView的简单实现

效果图:

布局代码:


<LinearLayout 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=".Load_Web"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/go_to"
            android:text="前进"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/go_back"
            android:text="后退"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/refresh"
            android:text="刷新"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/pause"
            android:text="暂停"
            />
    LinearLayout>
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:id="@+id/pb"
        />
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/load_web"
        >

    WebView>
LinearLayout>

java代码:

package com.example.mywebdemo;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class Load_Web extends AppCompatActivity implements View.OnClickListener {
    private WebView webView;
    private ProgressBar pb;
    private String urlStr = "https://tech.meituan.com/WebViewPerf.html";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load__web);
        initView();
        initWebData();
    }

    private void initWebData() {
        webView.loadUrl(urlStr);

        //设置加载
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                //页面加载时显示进度条
                pb.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //页面加载完毕时隐藏进度条
                pb.setVisibility(View.GONE);
            }
        });

        //添加进度条  展示进度条的进度
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                pb.setProgress(newProgress);
            }
        });

    }

    private void initView() {
        webView = findViewById(R.id.load_web);
        //设置点击事件
        findViewById(R.id.go_to).setOnClickListener(this);
        findViewById(R.id.go_back).setOnClickListener(this);
        findViewById(R.id.refresh).setOnClickListener(this);
        findViewById(R.id.pause).setOnClickListener(this);
        //获取进度条
        pb = findViewById(R.id.pb);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.go_to://前进
                //判断页面是否能够前进
                if(webView.canGoForward()){
                    webView.goForward();
                }else{
                    Toast.makeText(this, "已是最前页面,无法前进", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.go_back://后退
                //判断页面是否能够后退
                if(webView.canGoBack()){
                    webView.goBack();
                }else{
                    Toast.makeText(this, "已是最初页面,无法后退", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.refresh://刷新
                //刷新页面方法调用
                webView.reload();
                break;
            case R.id.pause://暂停
                //停止加载
                webView.stopLoading();
                break;
        }
    }
}

你可能感兴趣的:(WebView的简单实现)