Android_WebView显示Web页面

本文介绍了如何在Android应用中使用WebView加载并显示Web页面。详细讲解了XML布局文件的配置以及MainActivity.java中的关键代码实现,帮助开发者理解如何在Android环境中集成WebView组件来展示网页内容。

XMl文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android_webview.MainActivity" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        />
</RelativeLayout>

MainActivity.java

package com.example.android_webview;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    private WebView webView;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        init();
    }


    private void init() {
        //webView加载web文件
        webView.loadUrl("http://www.baidu.com");
        //webview加载本地文件
        //webView.loadUrl("file:///android_asset/ZuoYe.html");
        //覆盖webview默认通过第三方或者系统浏览器打开网页的行为,使得网页在webview中打开
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值为true,在webview中打开,返回值为false在第三方或者系统浏览器打开网页
                view.loadUrl(url);
                return true;
            }
        });
        //启用支持javaScript
        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) {
                // TODO Auto-generated method stub
                //newProgress 1-100之间的整数
                if(newProgress==100){
                    //网页加载完毕,关闭progressDialog
                    closeDialog(newProgress);
                }
                else{
                    //网页正在加载,打开progressDialog
                    openDialog(newProgress);
                }
            }


            private void openDialog(int newProgress) {
                // TODO Auto-generated method stub
                if(dialog==null){
                    dialog = new ProgressDialog(MainActivity.this);
                    dialog.setTitle("正在加载:");
                    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    dialog.setProgress(newProgress);
                    dialog.show();
                }
                else{
                    dialog.setProgress(newProgress);
                }
            }

            private void closeDialog(int newProgress) {
                // TODO Auto-generated method stub
                if(dialog!=null&&dialog.isShowing()){
                    dialog.dismiss();
                    dialog=null;
                }
            }
        });
    }

    //改写物理按键--返回的逻辑
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(webView.canGoBack()){
                webView.goBack();
                return true;
            }
            else{
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值