提示导致UI在webView中锁定(使用.setWebChromeClient(新的WebChromeClient()和片段对话框)

时间:2016-10-17 23:30:29

标签: android android-fragments android-webview alertdialog dialogfragment

我有一个裸项目,只有webview和警报和提示的实现...每个都可以正常工作,但如果提示立即在警报内,UI停止响应:alert(提示(" hi" ," test"));

有什么可能导致它的想法吗?请在下面找到代码的相关部分:

public class MainActivity extends AppCompatActivity implements JSDialogFragment.JSDialogResultListener{
    private WebView m_webView;

    public void OnJsDialogFragmentPositive(Bundle args, Object cb) {
        Log.d("JSDialogFragment", "OnJsDialogFragmentPositive");
        jsDialogType dType = (jsDialogType)args.getSerializable("dType");
        if(null != dType && null != cb) {
            if(JS_DIALOG_TYPE_ALERT == dType) {
                ((JsResult) cb).confirm();
            } else if(JS_DIALOG_TYPE_PROMPT == dType) {
                String value = args.getString("value");
                ((JsPromptResult) cb).confirm(null != value ? value : "");
            }
        }
    }

    public void OnJsDialogFragmentNegative(Bundle args, Object cb) {
        Log.d("JSDialogFragment", "OnJsDialogFragmentNegative");
    }

    m_webView = (WebView) findViewById(R.id.m_webView);
    m_webView.getSettings().setJavaScriptEnabled(true);

    m_webView.setWebChromeClient(new WebChromeClient() {

        // shared set up transaction code
        FragmentTransaction showJsTransactionInit() {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            android.app.Fragment prev = getFragmentManager().findFragmentByTag("mytag");
            if (prev != null) ft.remove(prev);
                ft.addToBackStack(null);
                    return ft;
                }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
                FragmentTransaction ft = showJsTransactionInit(); 
                DialogFragment frag = JSDialogFragment.newInstanceAlert(message, result);
                frag.show(ft, "mytag");
                return true; // handled
            }

            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final android.webkit.JsPromptResult result) {
                FragmentTransaction ft = showJsTransactionInit();
                DialogFragment frag = JSDialogFragment.newInstancePrompt(message, result);
                frag.show(ft, "mytag");
                return true; // handled
            }
        });

和处理对话框的JSDialogFragment实现:

public class JSDialogFragment extends DialogFragment {
    interface JSDialogResultListener {
        void OnJsDialogFragmentPositive(Bundle obj, Object cb);
        void OnJsDialogFragmentNegative(Bundle obj, Object cb);
    }

    private Object mCB;

    public void setCallBack(Object cb) {
        mCB = cb;
    }

    // to init alert incase it may differ from how we show prompt
    public static DialogFragment newInstanceAlert(String message, Object cb) { 
        JSDialogFragment frag = new JSDialogFragment();
        frag.setCallBack(cb);
        Bundle args = new Bundle();
        args.putString("caption", message);
        args.putSerializable("dType", jsDialogType.JS_DIALOG_TYPE_ALERT); (JS_DIALOG_TYPE_ALERT is just an enum to distinguish between the types)
        frag.setArguments(args);
        return frag;
    }

    // to init prompt incase it may differ from how we show alert
    public static DialogFragment newInstancePrompt(String message, Object cb) { 
        JSDialogFragment frag = new JSDialogFragment();
        frag.setCallBack(cb);
        Bundle args = new Bundle();
        args.putString("caption", message);
        args.putSerializable("dType", jsDialogType.JS_DIALOG_TYPE_PROMPT);
        frag.setArguments(args);
        return frag;
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof JSDialogResultListener)) {
            throw new ClassCastException(activity.toString() + " must implement JSDialogResultListener");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Bundle args = getArguments();
        String caption = args.getString("caption");
        jsDialogType jsDType = (jsDialogType)args.getSerializable("dType");
        if(null==jsDType) jsDType = jsDialogType.JS_DIALOG_TYPE_ALERT;

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) //, R.style.jsDialogsTheme)
                .setTitle(R.string.jsDialogTitle)
                .setMessage(caption);

        switch(jsDType) {
            case JS_DIALOG_TYPE_ALERT:
                builder.setPositiveButton(android.R.string.ok , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((JSDialogResultListener) getActivity()).OnJsDialogFragmentPositive(args, mCB);
                        dialog.dismiss();
                    }
                });
                break;


            case JS_DIALOG_TYPE_PROMPT:
                builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        args.putString("value", "waaaawwweeeee3");
                        ((JSDialogResultListener) getActivity()).OnJsDialogFragmentPositive(args, mCB);
                        dialog.dismiss();
                    }
                }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((JSDialogResultListener) getActivity()).OnJsDialogFragmentNegative(args, mCB);
                        dialog.dismiss();
                    }
                });
                break;
        }

        return builder.create();
    }
}

有什么想法,建议吗?

0 个答案:

没有答案
相关问题