天天看点

webview加载html跳转,WebView加载网页(二)

WebView加载网页(二)

一、实现目标

1、实现一个页面activity_main.xml,该页面上面有一个TextView和两个WebView,一个WebView显示百度首页,另一个WebView显示另外一个网站的首页。

二、步骤

1、新建项目

使用Android Studio新建一个项目

2、制作页面activity_main.xml

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"

android:orientation="vertical"

tools:context=".MainActivity">

android:layout_width="wrap_content"

android:layout_height="0dp"

android:layout_weight="3"

android:text="Hello World!"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent"

android:textSize="30sp"/>

android:layout_width="match_parent"

android:background="#030303"

/>

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="5"

android:id="@+id/webview1"

>

android:layout_width="match_parent"

android:background="#030303"

/>

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="5"

android:id="@+id/webview2"

>

效果如下:

webview加载html跳转,WebView加载网页(二)

3、修改MainActivity.java

package cn.qiu.test03;

import android.app.Activity;

import android.app.ProgressDialog;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.webkit.WebChromeClient;

import android.webkit.WebView;

import android.webkit.WebViewClient;

public class MainActivity extends Activity {

private WebView webView1;

private WebView webView2;

private ProgressDialog pDialog;

private long exitTime = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init_webview1();

init_webview2();

}

private void init_webview1(){

webView1 = (WebView) findViewById(R.id.webview1);

webView1.setWebViewClient(new WebViewClient() {

//设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);

return true;

}

});

webView1.getSettings().setJavaScriptEnabled(true); //设置WebView属性,运行执行js脚本

webView1.loadUrl("https://www.baidu.com/"); //调用loadUrl方法为WebView加入链接

webView1.setWebViewClient(new WebViewClient(){ //

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

try{

if(url.startsWith("baiduboxapp://")||url.startsWith("baiduboxlite://")){

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

startActivity(intent);

return true;

}

}catch (Exception e){

return false;

}

webView1.loadUrl(url);

return true;

}

});

//setContentView(webView); //调用Activity提供的setContentView将webView显示出来

//页面加载的进度

webView1.setWebChromeClient(new WebChromeClient(){

@Override

public void onProgressChanged(WebView view, int newProgress) {

//newProgress为1~100之间的整数

if(newProgress==100){

//网页加载完毕,关闭ProgressDialog

closeDialog();

}else{

//网页正在加载,打开ProgressDialog

openDialog(newProgress);

}

}

private void closeDialog() {

//进度条不为空并且显示有进度条时

if(pDialog!=null&&pDialog.isShowing()){

pDialog.dismiss();//进度条取消显示

pDialog=null;//并且进度条设置为空

}

}

private void openDialog(int newProgress) {

//进度条为空时

if (pDialog==null){

pDialog=new ProgressDialog(MainActivity.this);

pDialog.setTitle("正在加载...");

//进度条的样式

pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

pDialog.setProgress(newProgress);//显示进度条的进度

pDialog.show();//显示进度条

}else {

pDialog.setProgress(newProgress);//显示最新(刷新)的进度

}

}

});

}

private void init_webview2(){

webView2 = (WebView) findViewById(R.id.webview2);

webView2.setWebViewClient(new WebViewClient() {

//设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);

return true;

}

});

webView2.getSettings().setJavaScriptEnabled(true); //设置WebView属性,运行执行js脚本

webView2.loadUrl("https://blog.csdn.net/"); //调用loadUrl方法为WebView加入链接

webView2.setWebViewClient(new WebViewClient(){ //

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

try{

if(url.startsWith("baiduboxapp://")||url.startsWith("baiduboxlite://")){

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

startActivity(intent);

return true;

}

}catch (Exception e){

return false;

}

webView2.loadUrl(url);

return true;

}

});

//setContentView(webView); //调用Activity提供的setContentView将webView显示出来

//页面加载的进度

webView2.setWebChromeClient(new WebChromeClient(){

@Override

public void onProgressChanged(WebView view, int newProgress) {

//newProgress为1~100之间的整数

if(newProgress==100){

//网页加载完毕,关闭ProgressDialog

closeDialog();

}else{

//网页正在加载,打开ProgressDialog

openDialog(newProgress);

}

}

private void closeDialog() {

//进度条不为空并且显示有进度条时

if(pDialog!=null&&pDialog.isShowing()){

pDialog.dismiss();//进度条取消显示

pDialog=null;//并且进度条设置为空

}

}

private void openDialog(int newProgress) {

//进度条为空时

if (pDialog==null){

pDialog=new ProgressDialog(MainActivity.this);

pDialog.setTitle("正在加载...");

//进度条的样式

pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

pDialog.setProgress(newProgress);//显示进度条的进度

pDialog.show();//显示进度条

}else {

pDialog.setProgress(newProgress);//显示最新(刷新)的进度

}

}

});

}

}

4、在AndroidManfest.xml中加入上网权限

三、测试

运行程序,显示效果如下: