1. 程式人生 > >android html5 的彈出窗設定

android html5 的彈出窗設定

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的東西。