1 设置webview与屏幕自适应:
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
2设置webview支持javaScript:
webView.getSettings().setJavaScriptEnabled(true);
3 webview与android交互:
getWebView().addJavascriptInterface(Object, String);
object:实例化一个对象,在html中js调用。
String:实例化对象的别名,js中通过此名老调用。
例如:
public class IndexWebview extends DetailFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=super.onCreateView(inflater, container, savedInstanceState);
getWebView().addJavascriptInterface(new Jsmenthod(getWebView(),getHandler()), "myindex");
loadPage("index.html");
return view;
}
}
public class Jsmenthod {
private WebView webView;
private Handler handler;
public Jsmenthod() {
// TODO Auto-generated constructor stub
}
public Jsmenthod(WebView webView,Handler handler) {
this.webView=webView;
this.handler=handler;
}
@JavascriptInterface
public void getInitInfo(String callback){
String json=”json数据“;
final String callbackFunction =getCallBackFunction(callback, new Object[]{json});
// 启动线程,将页面加载加入队列
Log.d("callbackFunction------", callbackFunction);
handler.post(new Runnable() {
@Override
public void run() {
// 加载url
webView.loadUrl(callbackFunction);
}
});
}
/**
* 功能描述:页面上的回调方法
* @param callback 页面js方法
* @param object 传递参数数组
* @return
*/
public String getCallBackFunction(String callback,Object[] object){
String callBackFunction="javascript:" + callback + "(";
if(object==null || object.length==0){
callBackFunction=callBackFunction+")";
}else{
for (int i = 0; i < object.length; i++) {
if(i!=object.length-1){
if(object[i].getClass().equals(String.class)){
callBackFunction=callBackFunction+"'"+object[i]+"',";
}else{
callBackFunction=callBackFunction+object[i]+",";
}
}else{
if(object[i].getClass().equals(String.class)){
callBackFunction=callBackFunction+"'"+object[i]+"')";
}else{
callBackFunction=callBackFunction+object[i]+")";
}
}
}
}
return callBackFunction;
}
}
页面中javascript:
<script type="text/javascript">
$(document).ready(function(){
myindex.getInitInfo("setInit");
});
function setInit(jsonText){
var conlistId=$("#conlistId");
alert(jsonText)
var tmpJson = $.parseJSON(jsonText);
for ( var i = 0; i < tmpJson.length; i++) {
conlistId.append("<li><a href='#'>"+tmpJson[i].title+"</a><span class='rqi'>"+tmpJson[i].date+"</span></li>")
}
}
</script>
4 webview html页面上编写js时弹出alert对话框:
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message,
final JsResult result) {
// 构建一个Builder来显示网页中的alert对话框
Builder builder = new Builder(BaseActivity.this);
builder.setTitle(BaseConstant.tips);
builder.setMessage(message);
builder.setPositiveButton(BaseConstant.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
BaseActivity.this.finish();
}
});
builder.setNegativeButton(BaseConstant.cancle, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
builder.setCancelable(false);
builder.create();
builder.show();
return true;
}
});