0

我在 WebView 中有一个 HTML 表单。表单控件看起来像一个普通的按钮,但实际上它是一个带有透明背景的 html 文件。当我按下 HTML 提交按钮时,我希望结果在浏览器中打开,而不是在 web 视图中。目前它正在破坏我的布局的当前 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>

然后我使用 String.format(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);
4

1 回答 1

3

表单提交给服务器而不是浏览器。确保您在提交表单时使用了正确的 URL。

要解决您的问题,您必须覆盖WebViewClient.shouldOverrideUrlLoading(.. ):

webview.setWebViewClient( new WebViewClient() {
   public void shouldOverrideUrlLoading (WebView view, String url) {
       return false;
   }
});
于 2010-11-13T08:55:43.527 回答