Android中的WebView控件用法

在Android开发中,使用WebView控件,可以很方便的实现Web浏览器的功能。

WebView官方详细文档: http://developer.android.com/reference/android/webkit/WebView.html

使用WebView加载一个网页很简单:

    	WebView webView = new WebView(this);
    	webView.loadUrl("http://www.google.com/");

加载apk包中的一个html页面的方法:

webView.loadUrl("file:///android_asset/test.html");

加载手机本地的一个html页面的方法:

webView.loadUrl("content://com.android.htmlfileprovider/sdcard/test.html");

WebView中有很多设置,可以用来开启或禁用插件,JavaScript等:

    	webView.getSettings().setPluginsEnabled(true);
    	webView.getSettings().setJavaScriptEnabled(true);
    	webView.getSettings().setSupportMultipleWindows(true);


如果要监听WebView的各种通知,请求。可以使用一个自定义的WebViewClient:

    public class MyWebViewClient extends WebViewClient
    {
    	@Override
    	public boolean shouldOverrideUrlLoading(WebView view, String url) 
    	{
    		view.loadUrl(url);
    		return true;
    	}

		@Override
		public void doUpdateVisitedHistory(WebView view, String url,
				boolean isReload)
		{
			// TODO Auto-generated method stub
			super.doUpdateVisitedHistory(view, url, isReload);
		}

		@Override
		public void onFormResubmission(WebView view, Message dontResend,
				Message resend)
		{
			// TODO Auto-generated method stub
			super.onFormResubmission(view, dontResend, resend);
		}

		@Override
		public void onLoadResource(WebView view, String url)
		{
			// TODO Auto-generated method stub
			super.onLoadResource(view, url);
		}

		@Override
		public void onPageFinished(WebView view, String url)
		{
			// TODO Auto-generated method stub
			super.onPageFinished(view, url);
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon)
		{
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl)
		{
			// TODO Auto-generated method stub
			super.onReceivedError(view, errorCode, description, failingUrl);
		}

		@Override
		public void onReceivedHttpAuthRequest(WebView view,
				HttpAuthHandler handler, String host, String realm)
		{
			// TODO Auto-generated method stub
			super.onReceivedHttpAuthRequest(view, handler, host, realm);
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error)
		{
			// TODO Auto-generated method stub
			super.onReceivedSslError(view, handler, error);
		}

		@Override
		public void onScaleChanged(WebView view, float oldScale, float newScale)
		{
			// TODO Auto-generated method stub
			super.onScaleChanged(view, oldScale, newScale);
		}

		@Override
		public void onTooManyRedirects(WebView view, Message cancelMsg,
				Message continueMsg)
		{
			// TODO Auto-generated method stub
			super.onTooManyRedirects(view, cancelMsg, continueMsg);
		}

		@Override
		public void onUnhandledKeyEvent(WebView view, KeyEvent event)
		{
			// TODO Auto-generated method stub
			super.onUnhandledKeyEvent(view, event);
		}

		@Override
		public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event)
		{
			// TODO Auto-generated method stub
			return super.shouldOverrideKeyEvent(view, event);
		}
    }


然后将其设置到WebView中:

    	MyWebViewClient webViewClient=new MyWebViewClient();
    	webView.setWebViewClient(webViewClient);

如果是要自定义页面的显示,如:JavaScript提示框,网站图标,加载进度条等,则可以自定义一个WebChromeClient

    public class MyWebChromeClient extends WebChromeClient
    {
    	@Override
    	public void onProgressChanged(WebView view, int newProgress)
    	{
    		if(newProgress==100)
    		{
    			view.setVisibility(View.VISIBLE);
    		}
    	}

		@Override
		public Bitmap getDefaultVideoPoster()
		{
			// TODO Auto-generated method stub
			return super.getDefaultVideoPoster();
		}

		@Override
		public View getVideoLoadingProgressView()
		{
			// TODO Auto-generated method stub
			return super.getVideoLoadingProgressView();
		}

		@Override
		public void getVisitedHistory(ValueCallback callback)
		{
			// TODO Auto-generated method stub
			super.getVisitedHistory(callback);
		}

		@Override
		public void onCloseWindow(WebView window)
		{
			// TODO Auto-generated method stub
			super.onCloseWindow(window);
		}

		@Override
		public boolean onConsoleMessage(ConsoleMessage consoleMessage)
		{
			// TODO Auto-generated method stub
			return super.onConsoleMessage(consoleMessage);
		}

		@Override
		public void onConsoleMessage(String message, int lineNumber,
				String sourceID)
		{
			// TODO Auto-generated method stub
			super.onConsoleMessage(message, lineNumber, sourceID);
		}

		@Override
		public boolean onCreateWindow(WebView view, boolean dialog,
				boolean userGesture, Message resultMsg)
		{
			// TODO Auto-generated method stub
			return super.onCreateWindow(view, dialog, userGesture, resultMsg);
		}

		@Override
		public void onExceededDatabaseQuota(String url,
				String databaseIdentifier, long currentQuota,
				long estimatedSize, long totalUsedQuota,
				QuotaUpdater quotaUpdater)
		{
			// TODO Auto-generated method stub
			super.onExceededDatabaseQuota(url, databaseIdentifier, currentQuota,
					estimatedSize, totalUsedQuota, quotaUpdater);
		}

		@Override
		public void onGeolocationPermissionsHidePrompt()
		{
			// TODO Auto-generated method stub
			super.onGeolocationPermissionsHidePrompt();
		}

		@Override
		public void onGeolocationPermissionsShowPrompt(String origin,
				Callback callback)
		{
			// TODO Auto-generated method stub
			super.onGeolocationPermissionsShowPrompt(origin, callback);
		}

		@Override
		public void onHideCustomView()
		{
			// TODO Auto-generated method stub
			super.onHideCustomView();
		}

		@Override
		public boolean onJsAlert(WebView view, String url, String message,
				JsResult result)
		{
			// TODO Auto-generated method stub
			return super.onJsAlert(view, url, message, result);
		}

		@Override
		public boolean onJsBeforeUnload(WebView view, String url,
				String message, JsResult result)
		{
			// TODO Auto-generated method stub
			return super.onJsBeforeUnload(view, url, message, result);
		}

		@Override
		public boolean onJsConfirm(WebView view, String url, String message,
				JsResult result)
		{
			// TODO Auto-generated method stub
			return super.onJsConfirm(view, url, message, result);
		}

		@Override
		public boolean onJsPrompt(WebView view, String url, String message,
				String defaultValue, JsPromptResult result)
		{
			// TODO Auto-generated method stub
			return super.onJsPrompt(view, url, message, defaultValue, result);
		}

		@Override
		public boolean onJsTimeout()
		{
			// TODO Auto-generated method stub
			return super.onJsTimeout();
		}

		@Override
		public void onReachedMaxAppCacheSize(long spaceNeeded,
				long totalUsedQuota, QuotaUpdater quotaUpdater)
		{
			// TODO Auto-generated method stub
			super.onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota, quotaUpdater);
		}

		@Override
		public void onReceivedIcon(WebView view, Bitmap icon)
		{
			// TODO Auto-generated method stub
			super.onReceivedIcon(view, icon);
		}

		@Override
		public void onReceivedTitle(WebView view, String title)
		{
			// TODO Auto-generated method stub
			super.onReceivedTitle(view, title);
		}

		@Override
		public void onReceivedTouchIconUrl(WebView view, String url,
				boolean precomposed)
		{
			// TODO Auto-generated method stub
			super.onReceivedTouchIconUrl(view, url, precomposed);
		}

		@Override
		public void onRequestFocus(WebView view)
		{
			// TODO Auto-generated method stub
			super.onRequestFocus(view);
		}

		@Override
		public void onShowCustomView(View view, CustomViewCallback callback)
		{
			// TODO Auto-generated method stub
			super.onShowCustomView(view, callback);
		}
    	
    }

这些方法中,只需要实现自己需要的就可以了。

然后把自定义的client注册到WebView中去:

    	MyWebChromeClient webChromeClient=new MyWebChromeClient();
    	webView.setWebChromeClient(webChromeClient);



你可能感兴趣的:(Android中的WebView控件用法)