Android--WebView

package com.cnn.webview03;

import android.annotation.SuppressLint;
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;
import android.widget.ProgressBar;


@SuppressLint("SetJavaScriptEnabled") public class MainActivity extends Activity {
    private WebView webView;
    private ProgressBar progressBar;
    private ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        webView=(WebView) findViewById(R.id.webView1);
        webView.loadUrl("http://sports.163.com/");
        //WebViewClient就是帮助WebView处理各种通知、请求事件的,具体来说包括
        //onLoadResource、onPageStart、onPageFinish、
        //onReceiveError、onReceivedHttpAuthRequest
        webView.setWebViewClient(new WebViewClient(){
            @Override
            //shouldOverrideUrlLoading:对网页中超链接按钮的响应。当按下某个连接时WebViewClient会调用这个方法,并传递参数:按下的url。
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO 自动生成的方法存根
                view.loadUrl(url);
                return true;
            }
        });
        //setWebChromeClient主要处理解析,渲染网页等浏览器做的事情
        //WebChromeClient是辅助WebView处理Javascript的对话框,网站图标,网站title,加载进度等 
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO 自动生成的方法存根
                if(newProgress==100){
                    closeDialog();
                }
                else {
                    openDialog(newProgress);
                }
            }

            private void openDialog(int newProgress) {
                // TODO 自动生成的方法存根
                if(dialog==null){
                    dialog=new ProgressDialog(MainActivity.this);
                }
                dialog.setTitle("加载中。。。");
                dialog.setProgress(newProgress);
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.show();
            }

            private void closeDialog() {
                // TODO 自动生成的方法存根
                dialog.dismiss();
                dialog=null;
            }
        });
        
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO 自动生成的方法存根
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if (webView.canGoBack()) {
                webView.goBack();
                return true;
            } else {
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

posted @ 2015-12-23 15:50  在路上的白羊  阅读(168)  评论(0编辑  收藏  举报