安卓——WebView














example.html文件的位置

以下是代码

example.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>实例</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<a href="http://www.baidu.com">打开百度</a>
</body>
</html>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lenovo.webview">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

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="com.example.lenovo.webview.MainActivity">


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

    </WebView>

</LinearLayout>

MainActivity

package com.example.lenovo.webview;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {
    private WebView webView;
    private String url="http://2014.qq.com/";
    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加载本地资源
        //webView.loadUrl("file:///android_asset/example.html");
        //webView加载web资源
        webView.loadUrl(url);

        //覆盖WebView请求系统浏览器或者第三方浏览器打开网页,在WebView自己里面打开
        webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            //返回true在自己里面打开,返回false在第三方或者系统浏览器中打开
            view.loadUrl(url);
            return true;

        }
    });
        //WebViewClient帮助webview处理一些页面的控制和请求通知
        //启用JavaScript
        WebSettings settings=webView.getSettings();
        settings.setJavaScriptEnabled(true);

        //webView优先加载缓存
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        //打开一个网页是加载一个对话框,提示用户当前加载了白分之几
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                //newProgress是1%-100%
                if(newProgress==100){

                    closedialogue();
                }
                else{
                    openDialogue(newProgress);
                }
            }
            public void closedialogue(){

                if(dialog!=null&&dialog.isShowing()) {
                    dialog.dismiss();
                    dialog = null;
                }
            }
            private void  openDialogue(int newProgress){
                if(dialog==null){
                    dialog=new ProgressDialog(MainActivity.this);
                    dialog.setTitle("正在加载");
                    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    dialog.setProgress(newProgress);
                    dialog.show();
                }
                else
                    dialog.setProgress(newProgress);
            }
        });
    }



    //改写物理按键——返回的逻辑
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            Toast.makeText(this,webView.getUrl(),Toast.LENGTH_SHORT).show();
            if(webView.canGoBack())
            {
                webView.goBack();
                return true;
            }
            else
                System.exit(0);//程序退出
        }

        return super.onKeyDown(keyCode, event);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水之积也不厚,则其负大舟也无力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值