android webview总结

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;
}

});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值