Modify alert() title (Javascript in Android Webview)

前端 未结 3 1178
北恋
北恋 2021-02-15 18:45

Screenshot: The page at file://

Is there anyway to modify the alert box title? Any help will be greatly appreciated. :)

3条回答
  •  逝去的感伤
    2021-02-15 19:16

    There are 3 types of js alerts:

    • alert box - with an Ok button to proceed.
    • confirm box - with both OK and cancel button.
    • prompt box - get a value from the user and then select OK/Cancel.

    • Use onJsAlert for alertBox .
    • Use onJsConfirm for confirmBox .
    • Use onJsPrompt for promptBox

    I have added the sample code for onJsConfirm,

            webViewLayout.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
    
                AlertDialog dialog =new AlertDialog.Builder(view.getContext()).
                        setTitle("Confirm").
                        setIcon(ContextCompat.getDrawable(view.getContext(),R.drawable.image)).
                        setMessage(message).
                        setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.cancel();
                            }
                        }).
                        setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                FlashMessage("JS");
                                result.confirm();
                            }
                        })
                        .create();
                dialog.show();
                return true;
            }
        });
    

提交回复
热议问题