简体   繁体   English

android:在我的网页视图中打开弹出窗口

[英]android : Open pop-up window in my webview

I have webview in my application and I want it to open pop up windows when clicking on a link inside webview.我的应用程序中有 webview,我希望它在单击 webview 中的链接时打开弹出窗口。 I have added following code but it didn't work:-我添加了以下代码,但没有用:-

WebSettings webSettings = webViewPage.getSettings();    
webSettings.setJavaScriptEnabled(true);    
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

这就是我希望弹出窗口出现的方式 the popup should appear like this弹出窗口应该是这样的

I am answering my own question after 3 long years:经过 3 年的漫长岁月,我正在回答我自己的问题:

When a link is touched inside a webpage then depending on a webpage implementation there are two possible scenarios: 1) Link will be opened in same window.当在网页内触摸链接时,根据网页实现,有两种可能的情况:1) 链接将在同一窗口中打开。 2) Link will be opened in new window. 2) 链接将在新窗口中打开。

Well Its easy to handle 1st scenario using below code:那么使用下面的代码很容易处理第一个场景:

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

Overiding shouldOverrideUrlLoading inside WebViewClient implementation will open link in same window.在 WebViewClient 实现中覆盖 shouldOverrideUrlLoading 将在同一窗口中打开链接。

Now lets look at the 2nd case, where webpage is requesting a url to be open in new window.现在让我们看看第二种情况,网页请求在新窗口中打开一个 url。 For this case we need to tell our webview to support multiple windows like below:对于这种情况,我们需要告诉我们的 webview 支持多个窗口,如下所示:

webView.getSettings().setSupportMultipleWindows(true);

and then adding a new web chrome client to webview to get event when new window is requested by webpage然后添加一个新的 web chrome 客户端到 webview 以在网页请求新窗口时获取事件

webView.setWebChromeClient(new WebChromeClient() {


        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog,
                boolean isUserGesture, Message resultMsg) {



                WebView newWebView = new WebView(WebpageActivity.this);
                newWebView.getSettings().setJavaScriptEnabled(true);
                newWebView.getSettings().setSupportZoom(true);
                newWebView.getSettings().setBuiltInZoomControls(true);
                newWebView.getSettings().setPluginState(PluginState.ON);
                newWebView.getSettings().setSupportMultipleWindows(true);
                view.addView(newWebView);
                WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
                transport.setWebView(newWebView);
                resultMsg.sendToTarget();

                newWebView.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true;
                    }
                });

                return true;
            }
        }

    });

Cheers!!!干杯!!!

Add this line to your web view web.addJavascriptInterface(new Jscalls(this), "Android");将此行添加到您的网络视图web.addJavascriptInterface(new Jscalls(this), "Android");

Then add this line to your href in html that is loaded in the web view href="javascript:showAndroidToast('Data to be shown or the URL from which data is to be shown')"然后将此行添加到您在 web 视图中加载的 html 中的href href="javascript:showAndroidToast('要显示的数据或要显示数据的 URL')"

   public class Jscalls {
    Context mContext;

    Jscalls(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */ 
    @JavascriptInterface
            public void showToast(final String toast) {
                // Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
                if (NetworkConnection.isConnected(mContext)) {
                    ((Activity)mContext).runOnUiThread(new Runnable() {
                        public void run() {
                            dialog = new Dialog(mContext);
                            dialog.setTitle("Title");

                            dialog.setContentView(R.layout.dialog);
                            dialog.setCancelable(true);
                            // there are a lot of settings, for dialog, check them all out!
                            // set up text

                            WebView web = (WebView) dialog.findViewById(R.id.webVie);
                            web.getSettings().setJavaScriptEnabled(true);
                            /*web.clearHistory();
                            web.clearFormData();
                            web.clearCache(true);*/
                            web.setWebViewClient(new HelloWebViewClient());
                            web.setOnLongClickListener(new OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View v) {
                                    return true;
                                }
                                });
                            web.setLongClickable(false);
                            try{
                                web.loadUrl(Url.mainUrl 
                                        + toast);//Url to load data from in pop-up
                            }catch (Exception e) {
                                // TODO: handle exception
                                //e.printStackTrace();
                            }   


                            // dialog.setContentView(web);
                            dialog.show();
                        }
                    });

                    //web = null ;
                    // now that the dialog is set up, it's time to show it

                    /*
                     * dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
                     * R.drawable.ic_launcher);
                     */

                    // dialog.setFeatureDrawable
                } else {
                    LoginMainActivity.validateEmail("Alert!!",
                            "This feature requires wi-fi or internet connection.",
                            mContext);
                }
            }
            private class HelloWebViewClient extends WebViewClient {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            }
            Dialog dialog;
        }

If someone is facing issue like pop is not opening or there is black screen when you are trying to open js pop up in webview.如果有人遇到类似 pop 无法打开的问题,或者当您尝试在 webview 中打开 js 时出现黑屏。 Please try to change height and width of webview like:-请尝试更改 webview 的高度和宽度,例如:-

android:layout_width="fill_parent"
android:layout_height="fill_parent"

OR或者

 android:layout_width="match_parent"
android:layout_height="match_parent"

like this:-像这样:-

<WebView
 android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM