您的位置:首页 > 移动开发 > Android开发

android html5 的弹出窗设置

2016-05-16 22:32 399 查看
webSettings = webView.getSettings();
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new MyWebChromeClient());
这些是基础的没说的
class MyWebChromeClient extends WebChromeClient {
// * 覆盖默认的window.alert展示界面,避免title里显示为“:来自file:////”

@Override
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);

// 不需要绑定按键事件
// 屏蔽keycode等于84之类的按键
builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
return true;
}
});
// 禁止响应按back键的事件
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
result.confirm();// 因为没有绑定事件,需要强行confirm,否则页面会变黑显示不了内容。
return true;
}

/**
* 覆盖默认的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();
}
})
.setNegativeButton("取消", 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();
}
});
// 屏蔽keycode等于84之类的按键,避免按键后导致对话框消息而页面无法再弹出对话框的问题
builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
return true;
}
});
// 禁止响应按back键的事件
// builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
return true;

}在h5中可以直接alert

还有等待窗口
public class ProgressDialogUtil {

private static Dialog progressDialog;
private static int level;//等待框展示层级,只有level=0的时候才能关闭progressDialog

public static synchronized void showProgressDialog(Context context) {

if (progressDialog == null || context != getDialogBaseContext(progressDialog)) {
dismissProgressDialog();
progressDialog = new Dialog(context, R.style.SH_MyDialogStyle);
progressDialog.setContentView(R.layout.sh_progress_dialog_layout);
progressDialog.setCancelable(true);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
} else if (!progressDialog.isShowing()) {
progressDialog.show();
}
level++;
}

public static synchronized void dismissProgressDialog() {
level--;
if (level < 0) {
level = 0;
}
if (progressDialog != null && level == 0 && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}

}

private static Context getDialogBaseContext(Dialog dialog) {
if (dialog.getContext() instanceof ContextThemeWrapper) {
try {
Field mBase = dialog.getContext().getClass().getDeclaredField("mBase");
mBase.setAccessible(true);
return (Context) mBase.get(dialog.getContext());
} catch (Exception e) {
e.printStackTrace();
}
}
return dialog.getContext();
}
}
<style name="SH_MyDialogStyle" parent="android:Theme">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:layout_gravity">center</item>
<item name="android:windowIsFloating">true</item>
<item name="android:minWidth">200dp</item>
<item name="android:windowCloseOnTouchOutside">false</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.2</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
</style>
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@drawable/bg_numpicker"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp">

<TextView
android:id="@+id/sh_textView"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_weight="1"
android:gravity="center_vertical|left"
android:text="请稍候…"
android:textColor="@color/sh_text_black_tips"
android:textSize="13sp" />

<ProgressBar
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
/**
* js   调用请求等待框显示
*/
@JavascriptInterface
public void showLoding() {
ProgressDialogUtil.showProgressDialog(mContext);
}

/**
* js   调用请求等待框消失
*/
@JavascriptInterface
public void dissmissLoding() {
ProgressDialogUtil.dismissProgressDialog();
}
在js中进行调用。
明天写点js h5的东西。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息