• 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>

     结果:

  • 相关阅读:
    acm常见算法及例题
    检测数据库各实例session分布情况的sql
    检测数据库大小的sql
    检测数据库各实例session 阻塞 tree的sql
    Oracle 的自治事务 AUTONOMOUS TRANSACTION
    Oracle的 NULL 与 空字符串''
    数据泵 expdp 参数 consistent=y
    mysql 的列转行
    mysql 的行转列
    jboss 到oracle 数据库连接无效的问题
  • 原文地址:https://www.cnblogs.com/zlc364624/p/10726784.html
Copyright © 2020-2023  润新知