1. 程式人生 > >Android WebView巢狀Html 之一

Android WebView巢狀Html 之一

A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.

     loadDataWithBaseURL(baseUrl==null/"about:blank",……),baseUrl為null或"about:blank"指定相對路徑.
  WebView 主要呼叫三個方法:LoadUrl、LoadData、LoadDataWithBaseURL.
  1、LoadUrl            直接載入網頁、圖片並顯示.(本地或是網路上的網頁、圖片、gif)
  2、LoadData           顯示文字與圖片內容 (模擬器1.5、1.6)
  3、LoadDataWithBase  顯示文字與圖片內容(支援多個模擬器版本)

我的程式碼:

public class WebViewActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.webview);

  WebView webView = new WebView(this);
  webView = (WebView) findViewById(R.id.webView);
  
  // 如果訪問的頁面中有Javascript,則webview必須設定支援Javascript。
  webView.getSettings().setJavaScriptEnabled(true);
  // 觸控焦點起作用
  // requestFocus();//如果不設定,則在點選網頁文字輸入框時,不能彈出軟鍵盤及不響應其他的一些事件。
  // 取消滾動條
  // this.setScrollBarStyle();
  // 設定WevView要顯示的網頁:
  // 網際網路用:
   webView.loadUrl("

http://write.blog.csdn.net/postedit");
  // 本地檔案用:webView.loadUrl("file:///android_asset/XX.html");// 本地檔案存放在:assets檔案中
  
   webView.setWebViewClient(new webViewClient());
  

 }
 
 //監聽器
 class webViewClient extends WebViewClient{

  /**
   * 對網頁中超連結按鈕的響應
   * 當按下某個連線時WebViewClient會呼叫這個方法,並傳遞引數:按下的url
   */
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
   // TODO Auto-generated method stub
   return super.shouldOverrideUrlLoading(view, url);
  }
  
  @Override
  public void onLoadResource(WebView view, String url) {
   // TODO Auto-generated method stub
   super.onLoadResource(view, url);
  }

  @Override
  public void onPageFinished(WebView view, String url) {
   // TODO Auto-generated method stub
   super.onPageFinished(view, url);
  }

  @Override
  public void onPageStarted(WebView view, String url, Bitmap favicon) {
   // TODO Auto-generated method stub
   super.onPageStarted(view, url, favicon);
  }

  @Override
  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
   // TODO Auto-generated method stub
   super.onReceivedError(view, errorCode, description, failingUrl);
  }

  @Override
  public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
   // TODO Auto-generated method stub
   super.onReceivedHttpAuthRequest(view, handler, host, realm);
  }

 }
 
 
}



別人的程式碼:

  1. public class AboutView extends Activity implements OnTouchListener,
  2. OnClickListener {
  3. private Button btnAbout;
  4. private WebView aboutBrowser;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.about);
  9. btnAbout = (Button) this.findViewById(R.id.about_button);
  10. aboutBrowser = (WebView) this.findViewById(R.id.about_webkit);
  11. btnAbout.setOnTouchListener(this);
  12. btnAbout.setOnClickListener(this);
  13. // WebSettings s = aboutBrowser.getSettings();
  14. // s.setUseWideViewPort(true);
  15. // s.setJavaScriptEnabled(true);
  16. // s.setBlockNetworkImage(true);
  17. loadHTML();
  18. }
  19. @Override
  20. public boolean onTouch(View v, MotionEvent event) {
  21. return false;
  22. }
  23. @Override
  24. public void onClick(View v) {
  25. if(v == btnAbout) {
  26. //這裡是切換Activity
  27. //注意:每個Activity都要在AndroidManifest.xml中註冊
  28. Intent i = new Intent();
  29. i.setClass(AboutView.this, CiHaiView.class);
  30. this.startActivity(i);
  31. AboutView.this.finish();
  32. }
  33. }
  34. public void loadHTML() {
  35. String vStr = getText(R.string.vStr).toString();
  36. String vNum = getText(R.string.vNum).toString();
  37. final String htmlText = "<html>" +
  38. "<head></head>" +
  39. "<body>" +
  40. "<br/>" +
  41. "<table align=\"center\" border=\"0\" cellspacing=\"15\">" +
  42. "<tr align=\"center\"><td><img src=\"file:///android_asset/edict.png\" /></td>" +
  43. "<td>" +
  44. "<b><font size=\"5\">" + vStr +"</font></b><br/>" + vNum +
  45. "</td></tr>" +
  46. "</table>" +
  47. "         " +
  48. "</body></html>";
  49. aboutBrowser.loadDataWithBaseURL("about:blank", htmlText, "text/html", "utf-8", null);
  50. }
  51. }