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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wView = (WebView)findViewById(R.id.wv1);
ProxyBridge pBridge = new ProxyBridge();
wView.addJavascriptInterface(pBridge, "AliansBridge");
WebSettings wSet = wView.getSettings();
wSet.setJavaScriptEnabled(true);
wView.loadUrl("file:///android_asset/index.html");
}
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wView = (WebView)findViewById(R.id.wv1);
ProxyBridge pBridge = new ProxyBridge();
wView.addJavascriptInterface(pBridge, "AliansBridge");
WebSettings wSet = wView.getSettings();
wSet.setJavaScriptEnabled(true);
wView.loadUrl("file:///android_asset/index.html");
}
public class ProxyBridge {
public int one () {
return 1;
}
}
}
解释:
wView.addJavascriptInterface(pBridge, "AliansBridge");
上句代码是实例化一个方法为的是在html中的js中调用,第二个参数则是 实例化方法的别名,如果要使用这个pBridge,则在js中使用的名字就是AliansBridge。
WebSettings wSet = wView.getSettings();
wSet.setJavaScriptEnabled(true);
上面两句就是允许使用js的意思
wView.loadUrl(file:///android_asset/index.html);
就是在asset下放入一个index.html文件,这个文件包含js的代码,注意路径是“///”,这个android_asset不是文件夹的名字是系统自动生成的,其实就是asset文件夹的系统路径。
index.html 内容如下:
Java代码
<html><body>
<div id="output">Test page.</div>
<input type="submit" value="change to 1" onclick="document.getElementById('output').innerHTML = AliansBridge.one()" />
</body></html>
<html><body>
<div id="output">Test page.</div>
<input type="submit" value="change to 1" onclick="document.getElementById('output').innerHTML = AliansBridge.one()" />
</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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.wv1);
WebSettings webSettings = mWebView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
mWebView.loadUrl("file:///android_asset/demo.html");
}
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() {
mHandler.post(new Runnable() {
public void run() {
mWebView.loadUrl("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
Log.d("aa", message);
Log.d("url", url);
Log.d("result", ""+result);
result.confirm();
return super.onJsAlert(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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.wv1);
WebSettings webSettings = mWebView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
mWebView.loadUrl("file:///android_asset/demo.html");
}
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() {
mHandler.post(new Runnable() {
public void run() {
mWebView.loadUrl("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
Log.d("aa", message);
Log.d("url", url);
Log.d("result", ""+result);
result.confirm();
return super.onJsAlert(view, url, message, result);
}
其中上面的代码 也提到了怎么在android中调试js mWebView.setWebChromeClient(new MyWebChromeClient());
上面的代码来源于别人的实例。
3、还要注意在这个实例中demo.html和两个image应该在同一个文件夹asset下
//加载本项目内asset目录下的文件 wView.loadUrl("file:///android_asset/index.html");
//加载本地指定地址的文件(如sd卡内的index.html文件)
wView.loadUrl("content://com.android.htmlfileprovider/sdcard/index.html");
3.//加载指定远程网页地址文件 wView.loadUrl("http://***********");
WebView 提供 loadData(String data, String mimeType, String encoding) : 方法,该方法可以用于加载并显示HRML代码,实际应用过程中 存在一个问题,,当他加载包含中文的HTML内容时,WebView将会显示乱码.
幸好WebView还提供了一个 loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) : 方法,该方法是loadData()方法的增强版,它不会产生乱码.
loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) : 几个参数的简单说明.
data : 指定需要加载的HTML 代码.
mimeType :指定HTML 代码的 MIME类型.对于HTML代码可指定为text/html .
encoding :指定HTML 代码的代码编码所用的字符集.比如指定 UTF-8.
contentWebView = (WebView) findViewById(R.id.webview);
// 启用javascript
contentWebView.getSettings().setJavaScriptEnabled(true);
// 从assets目录下面的加载html
contentWebView.loadUrl("
file:///android_asset/wst.html
");
还有img标签的图片资源跟html一般在同一目录下