zoukankan      html  css  js  c++  java
  • WebView简单使用

    在布局文件中定义好webView,在Java代码中监听,webview本身就是一个浏览器,谷歌内嵌的浏览器,所以可以直接加载网页,webview也是使用JavaScript脚本语言的唯一途径

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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="500dp"></WebView>
    
    </RelativeLayout>

    上面是布局文件

    public class MainActivity extends AppCompatActivity {
        WebView webView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
           webView = findViewById(R.id.webView);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("https://www.baidu.com/?tn=62095104_19_oem_dg");
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    System.out.println("开始加载");
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    System.out.println("结束加载");
                }
    
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    
                    return super.shouldOverrideUrlLoading(view, request);
                }
            });
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onReceivedTitle(WebView view, String title) {
                    super.onReceivedTitle(view, title);
                    System.out.println("显示标题");
                }
    
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
                    System.out.println("显示加载进度" + newProgress + "%");
                }
            });
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                webView.goBack(); //点击返回上一页面而不是退出浏览器
                return true;
            }
    
            return super.onKeyDown(keyCode, event);
        }
    
    
        @Override
        protected void onDestroy() {
            if (webView != null) {
                webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
                webView.clearHistory();    //销毁Webview
    
                ((ViewGroup) webView.getParent()).removeView(webView);
                webView.destroy();
                webView = null;
            }
            super.onDestroy();
        }
    
    }

    最后不要忘了网络权限

  • 相关阅读:
    Go strings.Builder
    httprouter使用pprof
    大规模分布式系统的跟踪系统
    Yearning 介绍(SQL审核平台)
    Inception介绍(MySQL自动化运维工具)
    go 学习资源和GitHub库
    go 命令
    Redash 安装部署
    systemd 编写
    查看mysql 版本
  • 原文地址:https://www.cnblogs.com/Ocean123123/p/10960624.html
Copyright © 2011-2022 走看看