Android Studio [WebView]

WebViewActivity.java

package com.xdw.a122;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends AppCompatActivity {

    private WebView mWvMain;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        mWvMain=findViewById(R.id.wv);
        //加载本地HTML
        //mWvMain.loadUrl("file:///android_asset/work1.html");
        mWvMain.getSettings().setJavaScriptEnabled(true);           //允许JavaScript
        mWvMain.setWebViewClient(new MyWebViewClient());
        mWvMain.setWebChromeClient(new MyWebChromeClient());        //加载
        //mWvMain.evaluateJavascript();
        //mWvMain.loadUrl("");
        mWvMain.loadUrl("http://m.baidu.com");
    }
    class  MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {       //开始加载
            super.onPageStarted(view, url, favicon);
            Log.d("WebView","onPageStarted...");
        }

        @Override
        public void onPageFinished(WebView view, String url) {                     //加载完成
            super.onPageFinished(view, url);
            Log.d("WebView","onPageFinished...");
            mWvMain.loadUrl("javascript:alert('hello')");
        }
    }

    class MyWebChromeClient extends WebChromeClient{
        @Override
        public void onProgressChanged(WebView view, int newProgress) {             //显示加载进度
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {                  //显示标题
            super.onReceivedTitle(view, title);
            setTitle(title);
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {                         //在WbeView返回上一页
        if(keyCode==KeyEvent.KEYCODE_BACK && mWvMain.canGoBack()){                  //判断按键是否是返回键和能否返回上一步
            mWvMain.goBack();                                                       //执行返回
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

activity_web_view.xml

xml version="1.0" encoding="utf-8"?>
<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=".WebViewActivity">


    <WebView
        android:id="@+id/wv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">WebView>
LinearLayout>

 结果:

Android Studio [WebView]_第1张图片

 

转载于:https://www.cnblogs.com/zlc364624/p/10726784.html

你可能感兴趣的:(Android Studio [WebView])