Android混合开发
今年突然就要做混合开发了.开始觉得慌慌的.整理了一下今年春节到现在的感受.和各种坑.
公司首先需要做个工单管理.本人对于页面不是特别了解.然后部长一直在做页面.我负责集成在Android上.
第一个坑.写的页面在Android上有的功能不能实现,于是用了腾讯的X5内核流浪器.勉勉强强能用了.
第二个坑. 在Android里面调用页面上的Alert,Confirm,Prompt弹窗有网址显示.于是重写了webChromeClient.代码如下:
public class MyWebChromeClient extends WebChromeClient {
@Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
}
@Override
public boolean onCreateWindow(WebView view, boolean dialog,
boolean userGesture, Message resultMsg) {
return super.onCreateWindow(view, dialog, userGesture, resultMsg);
}
/**
* 覆盖默认的window.alert展示界面,避免title里显示为“:来自file:////”
*/
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(" ")
.setMessage(message)
.setPositiveButton("确定", null);
// 禁止响应按back键的事件
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
result.confirm();// 因为没有绑定事件,需要强行confirm,否则页面会变黑显示不了内容。
return true;
// return super.onJsAlert(view, url, message, result);
}
public boolean onJsBeforeUnload(WebView view, String url,
String message, JsResult result) {
return super.onJsBeforeUnload(view, url, message, result);
}
/**
* 覆盖默认的window.confirm展示界面,避免title里显示为“:来自file:////”
*/
public boolean onJsConfirm(WebView view, String url, String message,
final JsResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(" ")
.setMessage(message)
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
result.confirm();
}
})
.setNeutralButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
result.cancel();
}
});
// 禁止响应按back键的事件
// builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
return true;
// return super.onJsConfirm(view, url, message, result);
}
/**
* 覆盖默认的window.prompt展示界面,避免title里显示为“:来自file:////”
* window.prompt('请输入您的域名地址', '618119.com');
*/
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, final JsPromptResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(" ").setMessage(message);
final EditText et = new EditText(view.getContext());
et.setSingleLine();
et.setText(defaultValue);
builder.setView(et)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm(et.getText().toString());
}
})
.setNeutralButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
// 禁止响应按back键的事件
// builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
return true;
// return super.onJsPrompt(view, url, message, defaultValue,
// result);
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
@Override
public void onRequestFocus(WebView view) {
super.onRequestFocus(view);
}
}
未完待续;