Android 之WebView

WebView类有2个方法装载HTML代码

1-publicvoidloadData(Stringdata,StringmimeType,Stringencoding);

2-publicvoidloadDataWithBaseURL(StringbaseURL,Stringdata,StringmimeType,

Stringencoding,StringfailUrl);

其中LoadData参数含义如下:(包含中文的HTML代码会出现乱码)

data:HTML代码

mimeType:Mime类型一般为text/html

encoding:HTML代码的编码

loadDataWidthBaseURL方法的参数含义如下:(建议使用该方法装载HTML代码)

baseUrl:获取相对路径的根URL如果设为null默认值是about:blank

failUrl:如果HTML代码装载失败或为Null时,WebView控件会装载这个参数指定的URL

打开JavaScript功能

webView.getSettings().setJavaScriptEnabled(true);

设置处理JavaScript的引擎

webView.setWebChromeClient(newWebChromeClient);

public class Main extends Activity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		WebView webView = (WebView) findViewById(R.id.webview);
		String html = "<html>"
				+ "<body>"
				+ "图书封面<br>"
				+ "<table width='200' border='1' >"
				+ "<tr>"
				+ "<td><a onclick='alert(\"Java Web开发速学宝典\")' ><img style='margin:10px' src='http://images.china-pub.com/ebook45001-50000/48015/cover.jpg' width='100'/></a></td>"				
				+ "<td><a onclick='alert(\"大象--Thinking in UML\")' ><img style='margin:10px' src='http://images.china-pub.com/ebook125001-130000/129881/zcover.jpg' width='100'/></td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td><img style='margin:10px' src='http://images.china-pub.com/ebook25001-30000/27518/zcover.jpg' width='100'/></td>"
				+ "<td><img  style='margin:10px' src='http://images.china-pub.com/ebook30001-35000/34838/zcover.jpg' width='100'/></td>"
				+ "</tr>" + "</table>" + "</body>" + "</html>";

		 webView.loadDataWithBaseURL("图书名", html, "text/html", "utf-8", null);
		 webView.getSettings().setJavaScriptEnabled(true);
		 webView.setWebChromeClient(new WebChromeClient());
	
		 
	}
}

相关推荐