【问题标题】:WebView blocking pop up windows?WebView 阻止弹出窗口?
【发布时间】:2012-08-06 05:57:06
【问题描述】:

我正在使用WebView 浏览 pesopay.com,它工作正常,除非我按下提交按钮。使用谷歌浏览器等互联网浏览器会显示一个弹出窗口,它会确认您填写的信息。但是在我的 Android WebView 中,当我按下提交按钮时什么也没发生。我想是因为WebView 没有启用显示弹出窗口。

谁能指出哪里出了问题?

这是我的活动:

public class PaymentActivity extends Activity {

String amount1;     
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.payment);

         Bundle extras = getIntent().getExtras(); 
            if(extras !=null)
            {
                amount1 = extras.getString("amount1");

            }

        WebView web = (WebView) findViewById(R.id.web1);
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("javascript:window.onload = function(){setValue(\""+ amount1 +"\");};");
        web.loadUrl("file:///android_asset/www/index.html");   
    }    
}

我的网页:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript"> 

    function setValue(amount1) {
        myValue = amount1;
        document.getElementById("amount").value = myValue;

    }
</script>
<script type="text/javascript">  
    function rand ( n )
    {
        document.getElementById("orderRefId").value =  ( Math.floor ( Math.random ( ) * n + 1 ) );
    }
</script>
</head>

<body onLoad="rand(200000)">
        <!-- 
            Note: https://www.pesopay.com/b2c2/eng/payment/payForm.jsp for live payment URL
                  https://test.pesopay.com/b2cDemo/eng/payment/payForm.jsp for test payment URL
        -->
        <form method="POST" name="frmPayment" action="https://test.pesopay.com/b2cDemo/eng/payment/payForm.jsp">
        <table>
        <tbody>
        <tr>
            <td>Order Reference No. (your reference number for every transaction that has transpired):</td> 
            <td><input type="text" id="orderRefId" name="orderRef" value="Test-001"/></td>
        </tr>   
        <tr>
            <td>Amount:</td>
            <td><input type="text" name="amount" id="amount" value=""/></td>

        </tr>
        <tr>
            <td>Currency Code - "608" for Philippine Peso, "840" for US Dollar:</td>
            <td><input type="text" name="currCode" value="608"/></td>
        </tr>   
        <tr>
            <td>Language:</td>
            <td><input type="text" name="lang" value="E"/></td>
        </tr>   
        <tr>
            <td>Merchant ID (the merchant identification number that was issued to you - merchant IDs between test account and live account are not the same):</td> 
            <td><input type="text" name="merchantId" value="18056869"/></td>
        </tr>
        <tr>    
            <td>Redirect to a URL upon failed transaction:</td>
            <td><input type="text" name="failUrl" value="http://www.yahoo.com?flag=failed"/></td>
        </tr>   
        <tr>
            <td>Redirect to a URL upon successful transaction:</td>
            <td><input type="text" name="successUrl" value="http://www.google.com?flag=success"/></td>
        </tr>
        <tr>
            <td>Redirect to a URL upon canceled transaction:</td>
            <td><input type="text" name="cancelUrl" value="http://www.altavista.com?flag=cancel"/></td>
        </tr>
        <tr>
            <td>Type of payment (normal sales or authorized i.e. hold payment):</td> 
            <td><input type="text" name="payType" value="N"/></td>
        </tr>
        <tr>    
            <td>Payment Method - Change to "ALL" for all the activated payment methods in the account, Change to "BancNet" for BancNet debit card payments only, Change to "GCASH" for GCash mobile payments only, Change to "CC" for credit card payments only:</td>
            <td><input type="text" name="payMethod" value="ALL"/></td>
        </tr>
        <tr>    
            <td>Remark:</td>
            <td><input type="text" name="remark" value="Asiapay Test"/></td>
        </tr>
        <!--<tr>    
            <td>Redirect:</td>
            <td><input type="text" name="redirect" value="1"/></td>
        </tr>-->
        <tr>
            <td></td>
        </tr>   

        <input type="submit" value="Submit">

        </tbody>
        </table>    

        </form>

</body>
</html>

【问题讨论】:

    标签: android html android-webview popupwindow


    【解决方案1】:

    如果我说对了,你需要使用 JavaScriptInterface 从 Android 调用 JavaScript 函数。

    查看此链接可能会对您有所帮助:

    http://www.codeproject.com/Articles/392603/Android-addJavaScriptInterface

    或者检查一些网络设置: http://developer.android.com/reference/android/webkit/WebSettings.html#setSupportMultipleWindows%28boolean

    尝试将此代码放入 onCreate()

            webView.getSettings().setPluginsEnabled(true);
            webView.getSettings().setAllowFileAccess(true);
            webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    

    【讨论】:

    • 但它只是在 webview 中显示吐司。
    • 未显示在我的网页视图中的弹出窗口不是我创建的。它是我正在浏览的网站的弹出窗口。链接test.pesopay.com/b2cDemo/eng/payment/payForm2.jsp
    • 好的,我明白了,你在 onCreate() webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true) 上试过这个;
    • 或检查我们的其他设置,这可能会对您有所帮助developer.android.com/reference/android/webkit/…
    • +1。谢谢。 webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 为我工作。
    【解决方案2】:

    Webview中负责对话框的函数是WebChromeClient的onJsAlert

    这里是示例代码

      public class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult jsResult) {
            // you can create your own dialog here or just return true for no pop up. 
            return true;
        }
    }
    

    并将其添加到您的网络视图中:

    MyWebChromeClient myWebChromeClient = new MyWebChromeClient();
    webView.setWebChromeClient(myWebChromeClient);
    

    【讨论】:

      猜你喜欢
      • 2017-11-28
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      相关资源
      最近更新 更多