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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值