android:在我的webview中打开弹出窗口

时间:2014-04-26 08:37:53

标签: android android-webview popupwindow

我的应用程序中有webview,我希望它在单击webview中的链接时打开弹出窗口。我添加了以下代码,但它没有工作: -

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

That's how I want pop up window to appear 弹出窗口应该像这样显示

3 个答案:

答案 0 :(得分:20)

经过3年的漫长岁月后,我回答了自己的问题:

当在网页内触摸链接时,根据网页实施,有两种可能的情况: 1)链接将在同一窗口中打开。 2)链接将在新窗口中打开。

使用以下代码很容易处理第一种情况:

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

在WebViewClient实现中覆盖shouldOverrideUrlLoading将在同一窗口中打开链接。

现在让我们看看第二种情况,其中网页请求在新窗口中打开网址。对于这种情况,我们需要告诉我们的webview支持多个窗口,如下所示:

webView.getSettings().setSupportMultipleWindows(true);

然后将新的网络Chrome客户端添加到网页视图,以便在网页请求新窗口时获取活动

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;
            }
        }

    });

干杯!!!

答案 1 :(得分:2)

将此行添加到您的网络视图 web.addJavascriptInterface(新的Jscalls(this),“Android”);

然后将此行添加到您在网页视图中加载的html中的href href =“javascript:showAndroidToast('要显示的数据或要显示数据的网址')”

   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;
        }

答案 2 :(得分:0)

如果某人正面临着诸如pop不能打开的问题,或者当您尝试打开js在网络视图中弹出时黑屏。请尝试更改WebView的高度和宽度,例如:-

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

OR

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

像这样:-

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