WebView的侦听事件+进度条的小例子
Activity:
public class CourseOnLine extends Activity {private WebView webview;private ProgressDialog dialog;@Overrideprotected 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);}@Overrideprotected 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{@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;}@Overridepublic void onPageFinished(WebView view, String url) {dialog.dismiss();}@Overridepublic void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {// TODO Auto-generated method stubsuper.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>