WebView加载字符串
最近在做项目的时候,需要开发一个扫码后跳转到H5页面。可能会遇到扫出来时一段字符串。比如扫手机wifi后的结果(WIFI:T:WPA;S:admin;P:123456)怎么将字符显示在webView上面呢?通过下面代码即可实现效果。用户还可以根据自己的需求进行自行调整样式等。
/***
* 加载字符串 (如果是字符串那就外部包裹一层Html代码片段)
* @param webView webView控件
* @param content 文本内容
*/
private void setWebString(WebView webView, String content) {
StringBuffer sb = new StringBuffer();
//添加html代码块
sb.append("<html><head><meta http-equiv='content-type' content='text/html; charset=utf-8'>");
sb.append("<meta charset='utf-8' content='1'></head><body style='color: black;font-size:34px;'><p></p>");
sb.append(content);
//结束的html代码块
sb.append("</body></html>");
WebSettings settings = webView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
webView.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
}