【问题标题】:Android HTML button onclick eventAndroid HTML 按钮 onclick 事件
【发布时间】:2014-04-04 21:14:24
【问题描述】:

我在本地 HTML 页面中显示图像,并且有一个用于单击事件的按钮。单击此按钮时,我需要调用另一个活动。它正在工作。但问题是在另一个之后返回 HTML 页面,图像未显示且应用程序无法正常工作。

<script language="javascript">
function validClick() {
    valid.performClick();
    document.getElementById("ok").value = "start";
}
function refuseClick(){
    refuse.performClick();
    document.getElementById("no").value = "Je refuse";
}

<div>
<button type="button" id="ok"
    style="font-weight: 700; margin-right: 20px;" onclick="validClick();">Start</button>

</div>

HTML 页面包含图片:

<li><a class="box" href="products.html" data-transition="fade" > <img src="img/products.png" alt="Products"> <span class="text"> Products</span> 

主活动:

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_screen);


        WebView  webview = (WebView) findViewById(R.id.webview);
        webview.loadUrl("file:///android_asset/index.html");

        valid = new Button(this);
        valid.setOnClickListener(this);
        refuse = new Button(this);
        refuse.setOnClickListener(this);

     // Enablejavascript
        WebSettings ws = webview.getSettings();
        ws.setJavaScriptEnabled(true);
        // Add the interface to record javascript events
        webview.addJavascriptInterface(valid, "valid");
        webview.addJavascriptInterface(refuse, "refuse");

 }


    @Override
    public void onClick(View v) {
        if (v.equals(valid)) {

        Intent i = new Intent(this, CloudReco.class);
        startActivity(i);
        } else if (v.equals(refuse)) {
            //do Something else }
    }

【问题讨论】:

  • 你能澄清一下图像不显示和应用程序不工作吗?您列出的 HTML 中没有图片。
  • 你能检查编辑的代码吗
  • 我仍然不能确切地知道您所说的应用程序无法正常工作是什么意思,但我已经发布了一些可以稍微改进您的程序的提示。如果您仍然有问题,请发布问题的确切细节; “它不起作用”太模糊了! :-)

标签: android html android-activity android-webview android-websettings


【解决方案1】:

您需要检查您的代码的一些事项。

首先,在您的 HTML 中,顶部的 &lt;script&gt; 标记没有关闭。请在最后一个函数定义后添加&lt;/script&gt;

其次,在Java中,请在调用loadUrl之前配置您的WebView。应先注册设置和 JavaScript 接口。

第三,无需使用Button 小部件即可实现您想要的效果。像这样的东西应该是完全足够的:

webview.addJavaScriptInterface(new Object() {
    @JavaScriptInterface
    public void performClick() {
        Intent i = new Intent(this, CloudReco.class);
        startActivity(i);
    }
}, "valid");

和“拒绝”类似。

【讨论】:

    【解决方案2】:

    我用来传递大量 javascript 事件的下一个“奇怪”方法(最后一次使用是来自滚动事件,我还传递了连接的值):

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("button1Event")) {
                // button1 was clicked inside webview html
                Toast.makeText(context, "Button1WasClicked", Toast.LENGTH_SHORT).show();  
                return true; //(to avoid a default redirect to the url)
             } else if (url.contains("button2Event")) {
                // button2 was clicked inside webview html
                Toast.makeText(context, "Button2WasClicked", Toast.LENGTH_SHORT).show();  
                return true; //(to avoid a default redirect to the url)
             }
             return false;
          }
    }
    

    HTML 包含一个虚拟的&lt;a&gt; 元素,用于模拟 URLclick,其中 URL 是我们在 Native 代码中等待的 KEY:

    <!DOCTYPE html>
    <html>
    <body>
    <a id='yourLinkID'></a>
    
    <button onclick="myFunction('button1Event')">Button1</button>
    <button onclick="myFunction('button2Event')">Button2</button>
    
    <script>
    function myFunction(value) {
        document.getElementById('yourLinkID').href = 'testtest' + value; 
        document.getElementById('yourLinkID').click();
    // we set the wanted URL and simulate the click
    }
    </script>
    
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      相关资源
      最近更新 更多