android学习笔记之webview的简单使用

来源:互联网 发布:处方点评软件 编辑:程序博客网 时间:2024/05/17 09:06
package com.example.administrator.myexercise;import android.annotation.SuppressLint;import android.app.Activity;import android.app.ProgressDialog;import android.content.Intent;import android.net.Uri;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;/** * Created by Administrator on 2016/6/28 0028. */public class WebviewActivity extends Activity{    private String url = "http://www.hao123.com";    private WebView webView;    private ProgressDialog dialog;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.webview);        initViews();       /* Uri uri = Uri.parse(url);        Intent intent = new Intent(Intent.ACTION_VIEW,uri);        startActivity(intent);*/    }    @SuppressLint("WrongViewCast")    private void initViews(){        webView = (WebView) findViewById(R.id.my_webview);        //webview加载本地资源        //webView.loadUrl("file:///android_asset/qilai.html");        //webview加载外部资源        webView.loadUrl(url);        //覆盖Webview默认通过第三方或者系统浏览器打开网页的行为,使得网页可以在浏览器中打开        webView.setWebViewClient(new WebViewClient(){            @Override            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {                //返回值为true的时候控制网页在webview中打开,为false的时候调用浏览器打开                return true;            }            //WebviewClient帮助webview去处理一些页面控制和请求通知        });        WebSettings webSettings = webView.getSettings();        webSettings.setJavaScriptEnabled(true);        //设置优先使用缓存        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);        //设置加载进度        webView.setWebChromeClient(new WebChromeClient(){            @Override            public void onProgressChanged(WebView view, int newProgress) {                if(newProgress == 100){                    //加载完成                    closeDialog();                }else{                    //正在加载                    openDialog(newProgress);                }                //super.onProgressChanged(view, newProgress);            }        });    }    /**     * 打开对话框     * @param newProgress 加载的进度     */    private void openDialog(int newProgress){        if (dialog == null){            dialog = new ProgressDialog(WebviewActivity.this);            dialog.setTitle("正在加载");            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);            dialog.setProgress(newProgress);            dialog.show();        }else{            dialog.setProgress(newProgress);        }    }    /**     * 关闭对话框     */    private void closeDialog(){        if (dialog != null && dialog.isShowing()){            dialog.dismiss();            dialog = null;        }    }    /**     * 改写物理按键返回的逻辑     * @param keyCode     * @param event     * @return     */    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){            webView.goBack();            return true;        }        return super.onKeyDown(keyCode, event);    }}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <WebView        android:id="@+id/my_webview"        android:layout_width="match_parent"        android:layout_height="match_parent"></WebView></LinearLayout>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.administrator.myexercise">        <uses-permission android:name="android.permission.INTERNET"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme"        >        <activity            android:name=".WebviewActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

0 0
原创粉丝点击