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 = "" +
  38. "" +
  39. "" +
  40. "
    " +
  41. "" +
  42. "
  43. " +
  44. "
  45. " +
  46. "
  47. " +
  48. "" + vStr +"
    " + vNum +
  49. "
  50. " +
  51. "         " +
  52. "";

  53. aboutBrowser.loadDataWithBaseURL("about:blank", htmlText, "text/html", "utf-8", null);

  54. }
  55. }

你可能感兴趣的:(Android)