当前位置: 代码迷 >> Web前端 >> WebView的侦听事件+进度条的小例证
  详细解决方案

WebView的侦听事件+进度条的小例证

热度:261   发布时间:2012-08-25 10:06:20.0
WebView的侦听事件+进度条的小例子

Activity:

public class CourseOnLine extends Activity {
	private WebView webview;
	private ProgressDialog dialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		showDialog(0);
		super.onCreate(savedInstanceState);
        setContentView(R.layout.online);
		Intent intent=getIntent();
		Bundle data=intent.getExtras();
		String courseURL=data.getString("courseWebURL");
		
		webview=(WebView)findViewById(R.id.webview01);
		//设置WebView属性,能够执行JavaScript脚本
		webview.getSettings().setJavaScriptEnabled(true);
		//如果要播放Flash,需要加上这一句
		webview.getSettings().setPluginsEnabled(true);
		//加载URL内容
		webview.loadUrl(courseURL);
		MyWebViewClient myWebView=new MyWebViewClient();
		webview.setWebViewClient(myWebView);
	}
	@Override//设置回退
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if((keyCode==KeyEvent.KEYCODE_BACK) && webview.canGoBack()){
			webview.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		//实例化进度条对话框
		dialog=new ProgressDialog(this);
		/*//可以不显示标题
		dialog.setTitle("正在加载,请稍候!");*/
		dialog.setIndeterminate(true);
		dialog.setMessage("正在加载,请稍候!");
		dialog.setCancelable(true);
		return dialog;
	}
	private class MyWebViewClient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
		@Override
		public void onPageFinished(WebView view, String url) {
			dialog.dismiss();
		}
		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			// TODO Auto-generated method stub
			super.onReceivedError(view, errorCode, description, failingUrl);
			dialog.dismiss();
		}
	}

?layout:online.xml?

?

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

</LinearLayout>