WebView加载网页及进度条

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WedActivity extends AppCompatActivity {

    private ProgressBar progressBar;
    private WebView webView;

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


    }
    private void initview() {
        webView = (WebView) findViewById(R.id.wed);
        progressBar = (ProgressBar) findViewById(R.id.prog);
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");

        webView.loadUrl(url);
    }
    private void shezh() {
        WebSettings seting = webView.getSettings();
        seting.setJavaScriptEnabled(true);//设置webview支持javascript脚本

        // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                if (newProgress == 100) {
                    // 网页加载完成
                    progressBar.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    // 加载中
                    progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    progressBar.setProgress(newProgress);//设置进度值
                }

            }
        });
    }

}
//布局
 
  
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_wed"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lenovo.yuekaomoni.WedActivity">
    <ProgressBar
        android:id="@+id/prog"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:max="100"
        style="?android:attr/progressBarStyleHorizontal"/>

    <WebView
    android:id="@+id/wed"
        android:layout_below="@+id/prog"
    android:layout_width="match_parent"
    android:layout_height="match_parent">WebView>
RelativeLayout>
加权限
 
  
<uses-permission android:name="android.permission.INTERNET" />

你可能感兴趣的:(WebView加载网页及进度条)