【问题标题】:add action to button inside WebView in android studio在 android studio 中的 WebView 内向按钮添加操作
【发布时间】:2021-05-11 22:14:57
【问题描述】:

我创建了 HTML 支付表单并在本地添加到 android studio,它完美地加载到了 Webview 我的问题是当我按下付款表单中的按钮时无法进行交易

这是我的代码

        web = findViewById(R.id.payment);
        loadHtmlPage(web);
        web.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                // Here put your code to exit
     
            Log.i("Payment","The Request is "+'\n'+ request);
                return true;
       
            }
        });
    }

    /**
     * Loads html page with the content.
     */
    private void loadHtmlPage(WebView mweb){
        String htmlString = getHtmlFromAsset();
        if (htmlString != null) {
            htmlString = htmlString.replace("$AMOUNT$",amount);

            mweb.loadDataWithBaseURL(" ", htmlString, "text/html", "UTF-8", null);
        } else
            Toast.makeText(this, "no such page", Toast.LENGTH_LONG).show();
    }

    private String getHtmlFromAsset () {
        InputStream is;
        StringBuilder builder = new StringBuilder();
        String htmlString = null;
        try {
            is = getAssets().open("payment.html");
            if (is != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }

                htmlString = builder.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return htmlString;
    }

【问题讨论】:

    标签: android webview payment-gateway


    【解决方案1】:

    我终于找到了答案……

    webView.loadUrl("javascript:(function(){document.getElementById('buttonClick').click();})()");
    

    这里是完整的源代码

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class Huddle extends Activity {
    private WebView wv;
    EditText editText;
    TextView textView;
    Button button,buttonClick;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.main);
    
        editText = (EditText)findViewById(R.id.titlebar);
        textView = (TextView)findViewById(R.id.txt_html);
        button = (Button)findViewById(R.id.button);
        buttonClick = (Button)findViewById(R.id.buttonClick);
    
        wv = (WebView) findViewById(R.id.webview);
    
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setUserAgentString("Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
    
        wv.setWebChromeClient(new MyChromeClient());
        wv.loadUrl("file:///android_asset/www/index.html");
        wv.requestFocus();
    
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String txt = editText.getText().toString();
                wv.loadUrl("javascript:sayHelloFromJS('"+txt+"')");
            }
        });
        buttonClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                wv.loadUrl("javascript:(function(){document.getElementById('buttonClick').click();})()");
            }
        });
    }
    
    class MyJavaScriptInterface
    {
        @SuppressWarnings("unused")
        public void showHTML(final String html)
        {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Huddle.this.textView.setText(html);
                }
            });
        }
    }
    
    class MyChromeClient extends WebChromeClient{
    }
    
    class MyWebClient extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
    
        }
     }
    }
    

    index.html

    将此 index.html 添加到 assets\www\index.html

    <html>
    <script language="JavaScript">
    function sayHelloFromJS(value) {
      document.getElementById("namefromAndroid").value =   value;
    }
    
    function setHtml(){
      var HTML = ""+document.getElementById("namefromjs").value;
      window.HTMLOUT.showHTML(HTML);
    }
    
    function doAlert() {
     alert("JavaScript says: Hello Android !!! How are you?");
    
    }
    </script>
    </head>
    <body>
     <p> Enter your name here <input type="text" id="namefromjs" />
     <p> <input type="button" onclick= "setHtml()" value="Set Name in Android">
     <p> Name from Android is <input type="text" id="namefromAndroid" />
    
      <p> Button Click <input type="button" value="Click me" id="buttonClick" onclick= "doAlert()" />
    </body>
    </html>
    

    res\layouts\main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    
    <TextView
            android:layout_width="184dp"
            android:layout_height="22dp"
            android:id="@+id/txt_html" android:layout_gravity="left|center_vertical"/>
    <EditText
            android:layout_width="161dp"
            android:layout_height="wrap_content"
            android:id="@+id/titlebar" android:layout_gravity="left|center_vertical"/>
    <Button
            android:layout_width="129dp"
            android:layout_height="wrap_content"
            android:text="Add text to web"
            android:id="@+id/button"/>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click Event"
            android:id="@+id/buttonClick" android:layout_gravity="left|center_vertical"/>
    <WebView android:id="@+id/webview" android:layout_width="match_parent"
             android:layout_height="match_parent"/>
    </LinearLayout>
    

    如果你想动态url就改网址

    【讨论】:

    • 感谢您的回答,但我无法弄清楚如何让按钮执行操作,即使我在表单标题中添加操作属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多