WebView组件介绍

indeterminateTint="#e4472a"属性即可ProgressBar的颜色

什么是WebView?

WebView是一个使用WebKit引擎的浏览器组件。
在Android 4.3系统及其以下,内部采用Webkit渲染引擎,在Android 4.4开始,采用chromium 渲染引擎来渲染View的内容。

Google对浏览器引擎进行了封装,提供了丰富的Java接口,其中最重要的便是android.webkit.WebView控件
效果图


WebView组件介绍_第1张图片
image.png

//设置支持缩放 此功能用手机测试
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
//设置支持使用悬浮工具进行缩放
webView.getSettings().setUseWideViewPort(true);
主代码

package com.example.wed_text;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private WebView webview;
    private Button button6;
    private Button button5;
    private Button button4;
    private Button button2;
    private Button button7;
    private LinearLayout mLi;
    private ImageView iconview;
    private TextView name;
    private SeekBar seek;
    private View mInflate;
    private EditText mViewById;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLi = (LinearLayout) findViewById(R.id.tiao);
        iconview = (ImageView) findViewById(R.id.icon);
        name = (TextView) findViewById(R.id.name);
        seek = (SeekBar) findViewById(R.id.seek);
        webview = (WebView) findViewById(R.id.webview);
        button6 = (Button) findViewById(R.id.button6);
        button6.setOnClickListener(this);
        button5 = (Button) findViewById(R.id.button5);
        button5.setOnClickListener(this);
        button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);
        button7 = (Button) findViewById(R.id.button7);
        button7.setOnClickListener(this);
        webview.loadUrl("http://www.baidu.com");//来一个默认搜索
        webview.setWebViewClient(new WebViewClient() {//设置监听就会在此软件中打开
            //
////            @Override//处理链接  不写的话所有链接都会默认调用其他软件打开
////            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
////                webview.loadUrl(request.getUrl().toString());
////                return true;
////            }
//
            @Override//监听网页开始加载
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                mLi.setVisibility(View.VISIBLE);
                seek.setMax(100);//设置最大值
                seek.setProgress(0);//设置进度

            }

            @Override//监听网页加载结束
            public void onPageFinished(WebView view, String url) {
                //设置布局隐藏
                mLi.setVisibility(View.GONE);
            }
        });

        webview.setWebChromeClient(new WebChromeClient() {
            @Override//进度更改时
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                seek.setProgress(newProgress); //进度
            }

            @Override//获得网址标题
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                name.setText(title);
            }

            @Override//获得网址图片
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);
                iconview.setImageBitmap(icon);
            }

            @Override//获得js语句
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }
        });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button6://搜索
                mInflate = View.inflate(this, R.layout.baidu, null);
                mViewById = mInflate.findViewById(R.id.baiduoo);
                AlertDialog.Builder Builder = new AlertDialog.Builder(MainActivity.this);
                Builder.setTitle("请输入网址");
                Builder.setIcon(R.mipmap.ic_launcher_round);
                Builder.setView(mInflate);
                Builder.setPositiveButton("确定", new Dialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String url = mViewById.getText().toString().trim();
                        //判断输入的内容
                        if (url != null && !url.equals("")) {
                            boolean infotext = infotext(url);
                            if (infotext) {//数字和汉字
                                try {//我将访问手机版百度
                                    webview.loadUrl("https://m.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=" + URLEncoder.encode(url, "UTF-8") + "&rsv_pq=c1ab49ed000e568c&rsv_t=1109WqA4JkOggU9OU2gUeQBiEdojDm594tg1Naj7dhcFRYypDA%2B5Npl5a6Q&rqlang=cn&rsv_enter=1&rsv_sug3=6&rsv_sug1=4&rsv_sug7=101&rsv_sug2=0&inputT=2271&rsv_sug4=3027");
                                } catch (UnsupportedEncodingException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                //是字母然后判断字母拼接
                                if (url.indexOf("http://") == 0 && url.indexOf(".com") == 0) {
                                    //indexOf==0代表我输入的能容有这个字符串
                                    webview.loadUrl(url);
                                } else {
                                    //判断是否有.com这个字符串
                                 if(url.endsWith(".com")){
                                     webview.loadUrl("http://" + url);
                                 }else{
                                     webview.loadUrl("http://" + url + ".com");
                                 }
                                }
                            }

                        }
                    }
                });

                Builder.setNegativeButton("取消", null);
                Builder.show();
                break;
            case R.id.button5://前进
                if (webview.canGoForward()) {//是否可以前进
                    webview.goForward();
                } else {
                    Toast.makeText(this, "已经是最前面的页面了", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.button4://后退
                if (webview.canGoBack()) {//是否可以后退
                    webview.goBack();
                } else {
                    Toast.makeText(this, "已经是最后一个页面了", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.button2://停止
                webview.stopLoading();
                break;
            case R.id.button7://刷新
                webview.reload();
                break;
        }
    }

    private boolean infotext(String txt) {
        Pattern p = Pattern.compile("[0-9]*");//
        Matcher m = p.matcher(txt);//matcher匹配器
        if (m.matches()) {//matches相匹配的
            return true;
        }
        p = Pattern.compile("[a-zA-Z]");//正则表达式
        m = p.matcher(txt);
        if (m.matches()) {
            return false;
        }
        char[] c = txt.toCharArray();//把传过来的String转换成char字符数组
        for (int i = 0;i < c.length;i ++)//通过循环来判断获取的char的每一个元素
        {
            p = Pattern.compile("[\u4e00-\u9fa5]");
            m = p.matcher(c[i]+"");//每一个元素判断是否是汉字
            if (m.matches()) {
                return true;//是true
            }
        }
        return false;
    }

    //加上返回监听对话框
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {//当手机按键按下时
        if (keyCode == KeyEvent.KEYCODE_BACK) {//判断是返回键
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
    private long exitTime=0;//用来保存上一次的时间
    private void exit(){
        if((System.currentTimeMillis()-exitTime)>2000) {//判断上次按的秒速
            Toast.makeText(getApplicationContext(),
                    "再按一次退出程序", Toast.LENGTH_SHORT).show();
            Log.e("tag","System.currentTimeMillis()"+System.currentTimeMillis());
            exitTime = System.currentTimeMillis();
        }else{
                finish();//关闭页面
                System.exit(0);
            }

        }

    }

主布局




    

        

        

        

    

    
    

    

       

AlertDialog对话框布局




    
    

你可能感兴趣的:(WebView组件介绍)