当前位置: 代码迷 >> Web前端 >> webview 总结一
  详细解决方案

webview 总结一

热度:848   发布时间:2012-12-20 09:53:21.0
webview 小结一

main.xml

?

?

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

?

? ?activity:

? ?public class HelloWebViewClient extends Activity {

	WebView mWebView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
	     	 mWebView = (WebView) findViewById(R.id.webview);
	     	 final Activity activity = this;
			mWebView = (WebView) findViewById(R.id.webview);
			mWebView.getSettings().setJavaScriptEnabled(true);   
			//设置滚动条样式
			mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
			mWebView.setHorizontalScrollBarEnabled(false);   
			mWebView.getSettings().setSupportZoom(true);   
//			mWebView.getSettings().setBuiltInZoomControls(true);   
//			mWebView.setInitialScale(70);   
//			mWebView.setHorizontalScrollbarOverlay(true);  
			//WebChromeClient是辅助WebView处理的对话框,网站图标,网站title,加载进度等 
			//如果activity中的xml设置了  android:theme="@android:style/Theme.NoTitleBar",那么下面的代码设置看不到效果
			mWebView.setWebChromeClient(new WebChromeClient()
			{
				public void onProgressChanged(WebView view, int progress)
				{
					activity.setTitle("Loading...");
					activity.setProgress(progress*100);
					if (progress==100)//
					{
						activity.setTitle("Loading OK!");
					}
				}
			});
	    	//WebViewClient就是帮助WebView处理各种通知、请求事件的
			mWebView.setWebViewClient(new WebViewClient()
			{
				@Override
				public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
				{
					Log.i("MainActivity", description);
				}
				
				//是否让自己的webview加载url
				@Override
				public boolean shouldOverrideUrlLoading(WebView view, String url)
				{
					view.loadUrl(url);
					return true;
				}
			});
			mWebView.loadUrl("http://www.baidu.com");
		}
	//返回按钮
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
	        mWebView.goBack();
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
}

??