1、可以是用loadData,这种方法需要先将html文件读取出来,以字符串传入loadData,可以展示页面,但是不会引用css、js等文件。 2、使用loadUrl,不过需要注意,这里因为是使用本地数据,所以传入的url需要做些处理,例如: a、如果html文件存于a
WebView提供的loadData(String data , String mimeType , String encoding)方法,该方法可用于加载并显示HTML代码,但在实际使用过程中,当它加载包含中文HTML内容时,WebView将会显示乱码。
WebView还提供了一个loadDataWithBaseURL(String baseUrl , String data , String mimeType , String encoding , String historyUrl)方法,该方法是loadData(String data , String mimeType , String encoding)方法的增强版,它不会产生乱码。
1、data:指定需要加载的HTML代码。
2、mimeType:指定HTML代码的MIME类型,对于HTML代码可指定为text/html。
3、encoding:指定HTML代码编码所用的字符集。比如指定为GBK。
import androidosBundle;
import androidappActivity;
import androidviewMenu;
import androidwebkitWebView;
public class ViewHtml extends Activity {
WebView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutactivity_view_html);
//获取程序中的WebView组件
show = (WebView) findViewById(Ridshow);
StringBuilder sb = new StringBuilder();
//拼接一段HTML代码
sbappend("<html>");
sbappend("<head>");
sbappend("<title>欢迎你</title>");
sbappend("</head>");
sbappend("<body>");
sbappend("<h2>111</h2>");
sbappend("</body>");
sbappend("</html>");
//使用简单的loadData方法会导致乱码,可能是Android API的Bug
//showloadData(sbtoString(), "text/html", "utf-8");
//加载、并显示HTML代码
showloadDataWithBaseURL(null,sbtoString(), "text/html", "utf-8", null);
}
}
Android开发中可以用webview直接加载html文本,具体方法为:
1、首先在layout文件夹下的xml中 加入Java代码:
<WebView
android:id="@+id/wv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Java代码
public class jsWebDemo extends Activity {
/ Called when the activity is first created /
@Override
public void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutmain);
WebView wView = (WebView)findViewById(Ridwv1);
ProxyBridge pBridge = new ProxyBridge();
wViewaddJavascriptInterface(pBridge, "AliansBridge");
WebSettings wSet = wViewgetSettings();
wSetsetJavaScriptEnabled(true);
wViewloadUrl("file:///android_asset/indexhtml");
}
public class ProxyBridge {
public int one () {
return 1;
}
}
}
public class jsWebDemo extends Activity {
/ Called when the activity is first created /
@Override
public void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutmain);
WebView wView = (WebView)findViewById(Ridwv1);
ProxyBridge pBridge = new ProxyBridge();
wViewaddJavascriptInterface(pBridge, "AliansBridge");
WebSettings wSet = wViewgetSettings();
wSetsetJavaScriptEnabled(true);
wViewloadUrl("file:///android_asset/indexhtml");
}
public class ProxyBridge {
public int one () {
return 1;
}
}
}
解释:
wViewaddJavascriptInterface(pBridge, "AliansBridge");
上句代码是实例化一个方法为的是在html中的js中调用,第二个参数则是 实例化方法的别名,如果要使用这个pBridge,则在js中使用的名字就是AliansBridge。
WebSettings wSet = wViewgetSettings();
wSetsetJavaScriptEnabled(true);
上面两句就是允许使用js的意思
wViewloadUrl(file:///android_asset/indexhtml);
就是在asset下放入一个indexhtml文件,这个文件包含js的代码,注意路径是“///”,这个android_asset不是文件夹的名字是系统自动生成的,其实就是asset文件夹的系统路径。
indexhtml 内容如下:
Java代码
<html><body>
<div id="output">Test page</div>
<input type="submit" value="change to 1" onclick="documentgetElementById('output')innerHTML = AliansBridgeone()" />
</body></html>
<html><body>
<div id="output">Test page</div>
<input type="submit" value="change to 1" onclick="documentgetElementById('output')innerHTML = AliansBridgeone()" />
</body></html>
2上面是javascript调用android的方法,那么android也可以调用js的方法:
Java代码
public class webJsDemo extends Activity {
/ Called when the activity is first created /
private WebView mWebView;
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutmain);
mWebView = (WebView) findViewById(Ridwv1);
WebSettings webSettings = mWebViewgetSettings();
webSettingssetSavePassword(false);
webSettingssetSaveFormData(false);
webSettingssetJavaScriptEnabled(true);
webSettingssetSupportZoom(false);
mWebViewsetWebChromeClient(new MyWebChromeClient());
mWebViewaddJavascriptInterface(new DemoJavaScriptInterface(), "demo");
mWebViewloadUrl("file:///android_asset/demohtml");
}
final class DemoJavaScriptInterface {
DemoJavaScriptInterface() {
}
/
This is not called on the UI thread Post a runnable to invoke
loadUrl on the UI thread
/
public void clickOnAndroid() {
mHandlerpost(new Runnable() {
public void run() {
mWebViewloadUrl("javascript:wave()");
}
});
}
}
/
Provides a hook for calling "alert" from javascript Useful for
debugging your javascript
/
final class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub
Logd("aa", message);
Logd("url", url);
Logd("result", ""+result);
resultconfirm();
return superonJsAlert(view, url, message, result);
}
}
public class webJsDemo extends Activity {
/ Called when the activity is first created /
private WebView mWebView;
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutmain);
mWebView = (WebView) findViewById(Ridwv1);
WebSettings webSettings = mWebViewgetSettings();
webSettingssetSavePassword(false);
webSettingssetSaveFormData(false);
webSettingssetJavaScriptEnabled(true);
webSettingssetSupportZoom(false);
mWebViewsetWebChromeClient(new MyWebChromeClient());
mWebViewaddJavascriptInterface(new DemoJavaScriptInterface(), "demo");
mWebViewloadUrl("file:///android_asset/demohtml");
}
final class DemoJavaScriptInterface {
DemoJavaScriptInterface() {
}
/
This is not called on the UI thread Post a runnable to invoke
loadUrl on the UI thread
/
public void clickOnAndroid() {
mHandlerpost(new Runnable() {
public void run() {
mWebViewloadUrl("javascript:wave()");
}
});
}
}
/
Provides a hook for calling "alert" from javascript Useful for
debugging your javascript
/
final class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub
Logd("aa", message);
Logd("url", url);
Logd("result", ""+result);
resultconfirm();
return superonJsAlert(view, url, message, result);
}
其中上面的代码 也提到了怎么在android中调试js mWebViewsetWebChromeClient(new MyWebChromeClient());
上面的代码来源于别人的实例。
3、还要注意在这个实例中demohtml和两个image应该在同一个文件夹asset下
可以用js加载css。
webviewsetWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
src="file:///android_asset/xxxjs"
String baseUrl = "file:///android_asset";
viewloadDataWithBaseURL(baseUrl, strHtml, "text/html", "utf-8", null);
}
}
js文件如下:
function loadjscssfile(filename,filetype){
if(filetype == "js"){
var fileref = documentcreateElement('script');
filerefsetAttribute("type","text/javascript");
filerefsetAttribute("src",filename);
}else if(filetype == "css"){
var fileref = documentcreateElement('link');
filerefsetAttribute("rel","stylesheet");
filerefsetAttribute("type","text/css");
filerefsetAttribute("href",filename);
}
if(typeof fileref != "undefined"){
documentgetElementsByTagName("head")[0]appendChild(fileref);
}
}
调用方式:
loadjscssfile("testcss","css");
以上就是关于webview加载本地html 要求哪些标签全部的内容,包括:webview加载本地html 要求哪些标签、怎么用WebView加载本地html、Android开发中能不能用webview直接加载html文本等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)