当前位置: 代码迷 >> Web前端 >> WebView运用
  详细解决方案

WebView运用

热度:252   发布时间:2012-06-28 15:20:04.0
WebView使用

WebView使用:

Java代码??收藏代码
  1. /**?Called?when?the?activity?is?first?created.?*/??
  2. ????@Override??
  3. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  4. ????????super.onCreate(savedInstanceState);??
  5. ??
  6. ????????//?在标题栏上显示进度??
  7. ????????getWindow().requestFeature(Window.FEATURE_PROGRESS);??
  8. ??
  9. ????????//?定义WebView??
  10. ????????webview?=?new?WebView(this);??
  11. ????????setContentView(webview);??
  12. ??
  13. ????????//?滚动条风格??
  14. ????????webview.setScrollBarStyle(0);??
  15. ??
  16. ????????//?设置JS可用??
  17. ????????webview.getSettings().setJavaScriptEnabled(true);??
  18. ??
  19. ????????final?Activity?activity?=?this;??
  20. ??
  21. ????????/*?
  22. ?????????*?WebChromeClient类:用来辅助WebView处理JavaScript的对话框,网站图标,网站Title,加载进度等?
  23. ?????????*?通过setWebChromeClient调协WebChromeClient类?
  24. ?????????*/??
  25. ????????webview.setWebChromeClient(new?WebChromeClient()?{??
  26. ????????????//?加载进度中,100时停止??
  27. ????????????public?void?onProgressChanged(WebView?view,?int?progress)?{??
  28. ????????????????activity.setProgress(progress?*?100);??
  29. ????????????}??
  30. ??
  31. ????????????@Override??
  32. ????????????public?void?onReceivedTitle(WebView?view,?String?title)?{??
  33. ????????????????activity.setTitle(title);??
  34. ????????????}??
  35. ??
  36. ????????});??
  37. ??
  38. ????????/*?
  39. ?????????*?WebViewClient类:?用来辅助WebView处理各种通知,请求等事件的类?
  40. ?????????*?通过setWebViewClient设置WebViewClient类?
  41. ?????????*/??
  42. ????????webview.setWebViewClient(new?WebViewClient()?{??
  43. ????????????//?页面加载失败??
  44. ????????????public?void?onReceivedError(WebView?view,?int?errorCode,??
  45. ????????????????????String?description,?String?failingUrl)?{??
  46. ????????????????Toast.makeText(activity,?"异常:!?"?+?description,??
  47. ????????????????????????Toast.LENGTH_LONG).show();??
  48. ????????????}??
  49. ??
  50. ????????});??
  51. ????????webview.loadUrl(Url);??
  52. ??
  53. ????}??
  54. ??
  55. ????/*?
  56. ?????*?通过WebView的goBack(),goForward()方法设置其前进和后退?
  57. ?????*/??
  58. ????@Override??
  59. ????public?boolean?onKeyDown(int?keyCode,?KeyEvent?event)?{??
  60. ????????//?TODO?Auto-generated?method?stub??
  61. ????????if?(keyCode?==?KeyEvent.KEYCODE_BACK?&&?webview.canGoBack())?{??
  62. ????????????//?返回前一个页面??
  63. ????????????webview.goBack();??
  64. ????????????return?true;??
  65. ????????}??
  66. ????????return?super.onKeyDown(keyCode,?event);??
  67. ????}??

layout:

Xml代码??
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:orientation="vertical"??
  4. ????android:layout_width="fill_parent"??
  5. ????android:layout_height="fill_parent"??
  6. ????>??
  7. <TextView????
  8. ????android:layout_width="fill_parent"???
  9. ????android:layout_height="wrap_content"???
  10. ????android:text="@string/hello"??
  11. ????/>??
  12. </LinearLayout>??

添加权限:

<uses-permission android:name="android.permission.INTERNET"/>

?

转载至:http://www.cnblogs.com/jdsjlzx/archive/2011/07/25/2116355.html

<script></script>