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()); } }
相关推荐
dangai00 2020-07-18
xhpscdx 2020-05-31
chenxiangpeng 2020-04-30
ncuboy0wsq 2020-03-27
学习web前端 2020-03-23
APCDE 2020-03-04
歆萌 2020-03-03
csdnuuu 2020-01-04
水龙吟的备忘录 2019-11-18
icewizardry 2014-04-10
元元 2019-11-17
androidstudyroom 2019-11-08
czpaex 2011-09-11
OldSoldier 2011-08-18
wangdaiying 2011-05-26
qixiang0 2019-08-24