欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

WebView加載網(wǎng)頁(二)-創(chuàng)新互聯(lián)

WebView加載網(wǎng)頁(二)

一、實(shí)現(xiàn)目標(biāo)

1、實(shí)現(xiàn)一個(gè)頁面activity_main.xml,該頁面上面有一個(gè)TextView和兩個(gè)WebView,一個(gè)WebView顯示百度首頁,另一個(gè)WebView顯示另外一個(gè)網(wǎng)站的首頁。

創(chuàng)新互聯(lián)公司主營(yíng)丹寨網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都APP應(yīng)用開發(fā),丹寨h5小程序設(shè)計(jì)搭建,丹寨網(wǎng)站營(yíng)銷推廣歡迎丹寨等地區(qū)企業(yè)咨詢

二、步驟

1、新建項(xiàng)目
使用Android Studio新建一個(gè)項(xiàng)目

2、制作頁面activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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">

    <TextView
        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"/>

    <View  android:layout_height="10px"
        android:layout_width="match_parent"
        android:background="#030303"
        />

    <WebView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:id="@+id/webview1"
        ></WebView>

    <View  android:layout_height="10px"
        android:layout_width="match_parent"
        android:background="#030303"
        />

    <WebView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:id="@+id/webview2"
        ></WebView>

</LinearLayout>

效果如下:
WebView加載網(wǎng)頁(二)

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() {
                        //設(shè)置在webView點(diǎn)擊打開的新網(wǎng)頁在當(dāng)前界面顯示,而不跳轉(zhuǎn)到新的瀏覽器中
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                view.loadUrl(url);
                                return true;
                        }
                });
               webView1.getSettings().setJavaScriptEnabled(true);  //設(shè)置WebView屬性,運(yùn)行執(zhí)行js腳本
               webView1.loadUrl("https://www.baidu.com/");          //調(diào)用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);                           //調(diào)用Activity提供的setContentView將webView顯示出來
                //頁面加載的進(jìn)度
               webView1.setWebChromeClient(new WebChromeClient(){
                        @Override
                        public void onProgressChanged(WebView view, int newProgress) {
                                //newProgress為1~100之間的整數(shù)
                                if(newProgress==100){
                                        //網(wǎng)頁加載完畢,關(guān)閉ProgressDialog
                                        closeDialog();
                                }else{
                                        //網(wǎng)頁正在加載,打開ProgressDialog
                                        openDialog(newProgress);
                                }
                        }

                        private void closeDialog() {
                                //進(jìn)度條不為空并且顯示有進(jìn)度條時(shí)
                                if(pDialog!=null&&pDialog.isShowing()){
                                        pDialog.dismiss();//進(jìn)度條取消顯示
                                        pDialog=null;//并且進(jìn)度條設(shè)置為空
                                }
                        }

                        private void openDialog(int newProgress) {
                                //進(jìn)度條為空時(shí)
                                if (pDialog==null){
                                        pDialog=new ProgressDialog(MainActivity.this);
                                        pDialog.setTitle("正在加載...");
                                        //進(jìn)度條的樣式
                                        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                                        pDialog.setProgress(newProgress);//顯示進(jìn)度條的進(jìn)度
                                        pDialog.show();//顯示進(jìn)度條
                                }else {
                                        pDialog.setProgress(newProgress);//顯示最新(刷新)的進(jìn)度
                                }
                        }
                });
        }

        private void init_webview2(){
               webView2 = (WebView) findViewById(R.id.webview2);
               webView2.setWebViewClient(new WebViewClient() {
                        //設(shè)置在webView點(diǎn)擊打開的新網(wǎng)頁在當(dāng)前界面顯示,而不跳轉(zhuǎn)到新的瀏覽器中
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                view.loadUrl(url);
                                return true;
                        }
                });
               webView2.getSettings().setJavaScriptEnabled(true);  //設(shè)置WebView屬性,運(yùn)行執(zhí)行js腳本
               webView2.loadUrl("https://blog.csdn.net/");          //調(diào)用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);                           //調(diào)用Activity提供的setContentView將webView顯示出來
                //頁面加載的進(jìn)度
               webView2.setWebChromeClient(new WebChromeClient(){
                        @Override
                        public void onProgressChanged(WebView view, int newProgress) {
                                //newProgress為1~100之間的整數(shù)
                                if(newProgress==100){
                                        //網(wǎng)頁加載完畢,關(guān)閉ProgressDialog
                                        closeDialog();
                                }else{
                                        //網(wǎng)頁正在加載,打開ProgressDialog
                                        openDialog(newProgress);
                                }
                        }

                        private void closeDialog() {
                                //進(jìn)度條不為空并且顯示有進(jìn)度條時(shí)
                                if(pDialog!=null&&pDialog.isShowing()){
                                        pDialog.dismiss();//進(jìn)度條取消顯示
                                        pDialog=null;//并且進(jìn)度條設(shè)置為空
                                }
                        }

                        private void openDialog(int newProgress) {
                                //進(jìn)度條為空時(shí)
                                if (pDialog==null){
                                        pDialog=new ProgressDialog(MainActivity.this);
                                        pDialog.setTitle("正在加載...");
                                        //進(jìn)度條的樣式
                                        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                                        pDialog.setProgress(newProgress);//顯示進(jìn)度條的進(jìn)度
                                        pDialog.show();//顯示進(jìn)度條
                                }else {
                                        pDialog.setProgress(newProgress);//顯示最新(刷新)的進(jìn)度
                                }
                        }
                });
        }

}

4、在AndroidManfest.xml中加入上網(wǎng)權(quán)限

<uses-permission android:name="android.permission.INTERNET" />

三、測(cè)試
運(yùn)行程序,顯示效果如下:

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

分享標(biāo)題:WebView加載網(wǎng)頁(二)-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://chinadenli.net/article38/cophpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)外貿(mào)網(wǎng)站建設(shè)企業(yè)網(wǎng)站制作搜索引擎優(yōu)化App設(shè)計(jì)虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)