首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

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

2012-08-25 
WebView的侦听事件+进度条的小例子Activity:public class CourseOnLine extends Activity {private WebVie

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>

热点排行