当前位置: 代码迷 >> Web前端 >> android_webView加载.html资料
  详细解决方案

android_webView加载.html资料

热度:807   发布时间:2012-09-15 19:09:28.0
android_webView加载.html文件

首先在layout文件夹下的xml中 加入

Java代码?
  1. ?<WebView??
  2. android:id="@+id/wv1"??
  3. android:layout_width="fill_parent"??
  4. android:layout_height="fill_parent"??
  5. />??

?

Java代码?
  1. public?class?jsWebDemo?extends?Activity?{??
  2. ????/**?Called?when?the?activity?is?first?created.?*/??
  3. ????@Override??
  4. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  5. ????????super.onCreate(savedInstanceState);??
  6. ????????setContentView(R.layout.main);???
  7. ????????WebView?wView?=?(WebView)findViewById(R.id.wv1);??
  8. ??????????
  9. ????????ProxyBridge?pBridge?=?new?ProxyBridge();??
  10. ????????wView.addJavascriptInterface(pBridge,?"AliansBridge");??
  11. ??????????
  12. ????????WebSettings?wSet?=?wView.getSettings();??
  13. ????????wSet.setJavaScriptEnabled(true);??
  14. ??????????
  15. ????????wView.loadUrl("file:///android_asset/index.html");??
  16. ??????????
  17. ????}??
  18. ??
  19. ????public?class?ProxyBridge?{??
  20. ????????public?int?one?()?{??
  21. ????????????return?1;??
  22. ????????}??
  23. ????}??
  24. ??
  25. }??

? wView.addJavascriptInterface(pBridge, "AliansBridge");
上句代码是实例化一个方法为的是在html中的js中调用,第二个参数则是 实例化方法的别名,如果要使用这个pBridge,则在js中使用的名字就是AliansBridge。

?WebSettings wSet = wView.getSettings();
??????? wSet.setJavaScriptEnabled(true);
上面两句就是允许使用js的意思

wView.loadUrl(file:///android_asset/index.html);
就是在asset下放入一个index.html文件,这个文件包含js的代码,注意路径是“///”,这个android_asset不是文件夹的名字是系统自动生成的,其实就是asset文件夹的系统路径。?

?

index.html 内容如下

Java代码?
  1. <html><body>??
  2. <div?id="output">Test?page.</div>??
  3. <input?type="submit"?value="change?to?1"?onclick="document.getElementById('output').innerHTML?=?AliansBridge.one()"?/>??
  4. </body></html>??

?

?这里要注意 如果你只有一个方法需要被js调用

?ProxyBridge pBridge = new ProxyBridge();
??????? wView.addJavascriptInterface(pBridge, "AliansBridge");

如果你需要调用好多个方法呢 这里只要

?wView.addJavascriptInterface(this, "AliansBridge");

把整个android程序所在的类 本身实例化传递就可以了

这里呢可以参看附件中的实例riademo

你如果想继续扩展可以参看

http://www.ophonesdn.com/article/show/115

?

?2.上面是javascript调用android的方法,那么android也可以调用js的方法

Java代码?
  1. public?class?webJsDemo?extends?Activity?{??
  2. ????/**?Called?when?the?activity?is?first?created.?*/??
  3. ????private?WebView?mWebView;??
  4. ?????private?Handler?mHandler?=?new?Handler();?????
  5. ???????
  6. ????@Override??
  7. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  8. ????????super.onCreate(savedInstanceState);??
  9. ????????setContentView(R.layout.main);??
  10. ????????mWebView?=?(WebView)?findViewById(R.id.wv1);?????
  11. ??????????
  12. ????????WebSettings?webSettings?=?mWebView.getSettings();?????
  13. ????????webSettings.setSavePassword(false);?????
  14. ????????webSettings.setSaveFormData(false);?????
  15. ????????webSettings.setJavaScriptEnabled(true);?????
  16. ????????webSettings.setSupportZoom(false);?????
  17. ?????????
  18. ????????mWebView.setWebChromeClient(new?MyWebChromeClient());?????
  19. ????
  20. ????????mWebView.addJavascriptInterface(new?DemoJavaScriptInterface(),?"demo");?????
  21. ????
  22. ????????mWebView.loadUrl("file:///android_asset/demo.html");?????
  23. ????}?????
  24. ????
  25. ????final?class?DemoJavaScriptInterface?{?????
  26. ????
  27. ????????DemoJavaScriptInterface()?{?????
  28. ????????}?????
  29. ????
  30. ????????/**???
  31. ?????????*?This?is?not?called?on?the?UI?thread.?Post?a?runnable?to?invoke???
  32. ?????????*?loadUrl?on?the?UI?thread.???
  33. ?????????*/????
  34. ????????public?void?clickOnAndroid()?{????
  35. ??????????????
  36. ????????????mHandler.post(new?Runnable()?{?????
  37. ????????????????public?void?run()?{?????
  38. ????????????????????mWebView.loadUrl("javascript:wave()");?????
  39. ????????????????}?????
  40. ????????????});?????
  41. ????
  42. ????????}?????
  43. ????}?????
  44. ????
  45. ????/**???
  46. ?????*?Provides?a?hook?for?calling?"alert"?from?javascript.?Useful?for???
  47. ?????*?debugging?your?javascript.???
  48. ?????*/????
  49. ????final?class?MyWebChromeClient?extends?WebChromeClient?{?????
  50. ????????@Override??
  51. ????????public?boolean?onJsAlert(WebView?view,?String?url,?String?message,??
  52. ????????????????JsResult?result)?{??
  53. ????????????//?TODO?Auto-generated?method?stub??
  54. ????????????Log.d("aa",?message);????
  55. ????????????Log.d("url",?url);??
  56. ????????????Log.d("result",?""+result);??
  57. ??????????????
  58. ????????????result.confirm();???
  59. ????????return?super.onJsAlert(view,?url,?message,?result);??
  60. ??????????????
  61. ????????}??
  62. ??
  63. ??????????
  64. ????}?????

?

其中上面的代码 也提到了怎么在android中调试js? mWebView.setWebChromeClient(new MyWebChromeClient());??

上面的代码来源于别人的实例。

最后还要注意在这个实例中demo.html和两个image应该在同一个文件夹asset下

  相关解决方案