zoukankan      html  css  js  c++  java
  • 先眼熟WebView

    package com.example.test;
    
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.webkit.WebChromeClient;
    import android.webkit.WebResourceRequest;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    
    public class Main2Activity extends AppCompatActivity {
    private String url="http://www.baidu.com";
        private WebView webView;
        private ProgressDialog dialog;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
    /*        Uri uri=Uri.parse(url);
            Intent intent=new Intent(Intent.ACTION_VIEW,uri);
            startActivity(intent);*/
            init();
        }
        private void init() {
            webView= (WebView) findViewById(R.id.webView);
            //webView.loadUrl("file:///android_asset/example.html");
            webView.loadUrl(url);
            //覆盖webview默认第三方或者系统浏览器打开网页的行为,使得网页可在webview打开
            webView.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    //返回值是true的时候控制网页在Webview中打开,如果为false调用系统浏览器或三方浏览器打开
                    view.loadUrl(url);
                    return true;
                }
                //webviewClient帮助webview去处理一些页面控制或请求通知
            });
            //启用javaScrip
            WebSettings settings=webView.getSettings();
            settings.setJavaScriptEnabled(true);
            //WebView加载页面优先使用缓存加载
            settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            webView.setWebChromeClient(new WebChromeClient(){
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    //newProgress 1-100之间的整数
                    if (newProgress==100){
                        //网页加载完毕
                        closeDialog();
                    }else {
                        //网页正在加载
                        openDialog(newProgress);
                    }
                }
                private void closeDialog(){
                    if (dialog!=null&&dialog.isShowing()){
                        dialog.dismiss();
                        dialog=null;
                    }
    
                }
                private void openDialog(int newProgress) {
                    if (dialog==null){
                        dialog=new ProgressDialog(Main2Activity.this);
                        dialog.setTitle("正在加载");
                        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        dialog.setProgress(newProgress);
                        dialog.show();
                    }else {
                        dialog.setProgress(newProgress);
                    }
                }
            });
    
        }
        //改写物理按键返回的逻辑
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode==KeyEvent.KEYCODE_BACK){
               // Toast.makeText(Main2Activity.this, webView.getUrl(), Toast.LENGTH_SHORT).show();
                if (webView.canGoBack()){
                    webView.goBack();//返回上衣界面
                    return true;
                }else {
                    System.exit(0);
                }
            }
            return super.onKeyDown(keyCode, event);
        }
    }
  • 相关阅读:
    用Jenkins构建项目实战
    在Windows上安装和配置Jenkins
    java用XSSFWorkbook实现读写Excel
    Properties类操作.properties配置文件方法总结
    java读取配置文件的推荐方法getResource、getResourceAsStream
    PerformanceRunner (性能测试工具) V1.1.4.1 新版本已发布!
    抢先知!MobileRunner(app测试工具)V2.1.2新版本
    号外号外!自动化测试工具AutoRunner V4.2 新版本升级预告!
    泽众自动化测试框架AutoTestFramework产品即将首发!
    泽众自动化测试框架AutoTestFramework产品功能预览
  • 原文地址:https://www.cnblogs.com/jiang2538406936/p/5976422.html
Copyright © 2011-2022 走看看