75 Android WebView (五)

package com.example.android_webview_shouldoverrideurlloading;

import android.graphics.Bitmap;
import android.inputmethodservice.Keyboard.Key;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

/**
 * 添加 权限 <uses-permission android:name="android.permission.INTERNET"/>
 * 
 * @author Administrator
 * 
 */
public class MainActivity extends Activity {

	private WebView webView;

	@SuppressLint("ResourceAsColor")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		webView = (WebView) findViewById(R.id.webView1);

		// webView.setBackgroundColor(0);//先设置背景色为transparent
		// webView.setBackgroundResource(R.drawable.ic_launcher);//然后设置背景图片

		webView.setWebViewClient(new WebViewClient() {

			/**
			 * 2. 当前网页的链接仍在webView中跳转
			 */
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				view.loadUrl(url);
				return true;
			}

		});

		// 3. 页面加载进度
		webView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				setTitle("页面加载中,请稍候..." + newProgress + "%");
				setProgress(newProgress * 100);
				if (newProgress == 100) {
					setTitle(R.string.app_name);
				}
				super.onProgressChanged(view, newProgress);
			}
		});

		webView.setWebViewClient(new WebViewClient() {
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				Toast.makeText(MainActivity.this, "Oh no! " + description,
						Toast.LENGTH_SHORT).show();
			}

			// 开始
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				Toast.makeText(MainActivity.this, "-->>onPageStarted", 1)
						.show();

			}

			// 加载完毕
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				Toast.makeText(MainActivity.this, "-->>onPageFinished", 1)
						.show();
			}

			/*
			 * Notify the host application that the WebView will load the
			 * resource specified by the given url.
			 * 
			 * Parameters view The WebView that is initiating the callback. url
			 * The url of the resource the WebView will load.
			 * 
			 * (non-Javadoc)
			 * 
			 * @see
			 * android.webkit.WebViewClient#onLoadResource(android.webkit.WebView
			 * , java.lang.String)
			 */
			@Override
			public void onLoadResource(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onLoadResource(view, url);
				Toast.makeText(MainActivity.this, "-->>onLoadResource", 1)
						.show();

			}
			
			

		});
		
		

		// 加载Url
		webView.loadUrl("http://www.baidu.com");

	}

	// 1. back键控制网页后退
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (webView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK
				&& event.getRepeatCount() == 0) {
			webView.goBack();
			return true;
		}
		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;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值