代码之家  ›  专栏  ›  技术社区  ›  jax

按“提交”按钮时,在浏览器中打开WebView表单

  •  0
  • jax  · 技术社区  · 14 年前

    我在网络视图中有一个HTML表单。表单控件看起来像一个常规按钮,但实际上它是一个具有透明背景的html文件。当我按下HTML提交按钮时,我希望结果在浏览器中打开,而不是在webview中。目前它正在当前的网络视图中打开,这破坏了我的布局。

    我该怎么做?

    这是我的表格

    <string name="googlecheckout_html_button">
    <![CDATA[ 
    <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
    <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
    <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">
        <head>
        <title>Google Checkout Button</title>
        <script type=\"text/javascript\" language=\"javascript\">
            function pass() {
                return javaInterface.checkboxPass();
            }
        </script>
        </head>
        <body>
            <form style=\"width: 121px;margin-left: auto;margin-right: auto;\" onsubmit=\"return pass();\" action=\"https://%1$sapi/checkout/v2/checkoutForm/Merchant/%2$s\" id=\"BB_BuyButtonForm\" method=\"post\" name=\"BB_BuyButtonForm\">
                <input name=\"item_name_1\" type=\"hidden\" value=\"%3$s\" />
                <input name=\"item_description_1\" type=\"hidden\" value=\"%3$s\" />
                <input name=\"item_quantity_1\" type=\"hidden\" value=\"1\" />
                <input name=\"item_price_1\" type=\"hidden\" value=\"%4$s\" />
                <input name=\"item_currency_1\" type=\"hidden\" value=\"%5$s\" />
                <input name=\"_charset_\" type=\"hidden\" value=\"utf-8\" />
                <input type=\"hidden\" name=\"shopping-cart.items.item-1.merchant-private-item-data\" value=\"%6$s\" />
                <input alt=\"Pay With Google Checkout\" src=\"https://%1$sbuttons/buy.gif?merchant_id=%2$s&amp;w=121&amp;h=44&amp;style=trans&amp;variant=text&amp;loc=en_US\" type=\"image\" />
            </form>
        </body>
    </html>
    ]]>
    </string>
    

    然后我使用字符串。格式(arg…)用适当的值填充字段。

    这里是WebView

    String form = String.format(getString(R.string.googlecheckout_html_button), 
                            host,
                            merchantId,
                            item_name_1,
                            item_price_1,
                            item_currency_1,
                            private_item_data
                            );
    
                    if(Logging.DEBUG) Log.d(TAG, form);
    
                    browser = new WebView(ActivityActivate.this);
                    browser.setBackgroundColor(0);
                    browser.getSettings().setJavaScriptEnabled(true);
                    browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                    browser.getSettings().setSupportZoom(true);
                    browser.setWebViewClient(new WebViewClient() {
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view,
                                String url) {
                            return false;
                        }
                    });
                    //browser.getSettings().setLoadsImagesAutomatically(true);
    
                    browser.addJavascriptInterface(new JavascriptInterface(), "javaInterface");
                    //browser.loadData(header+formData+footer, "text/html", "UTF-8");
                    browser.loadDataWithBaseURL("https://checkout.google.com", form, "text/html", "UTF-8", null);
                    llPaymentButtons.addView(browser);
    
    1 回复  |  直到 14 年前
        1
  •  3
  •   Peter Knego    14 年前

    表单提交到服务器,而不是浏览器。确保提交表单的URL正确。

    要解决你的问题,你必须超越 WebViewClient.shouldOverrideUrlLoading(.. ):

    webview.setWebViewClient( new WebViewClient() {
       public void shouldOverrideUrlLoading (WebView view, String url) {
           return false;
       }
    });