开发者问题收集

我们如何从网页按钮点击事件导航到活动?

2019-04-10
426

我在 webview 中有两个按钮。我想手动为它们添加点击事件。 以下是我的代码;

网页代码

<form method="post" name="refer_now">
    <div class="refer-now-button">
      <!-- <button type="submit" class="refer-now-btn" id="refer-now">refer now</button> -->
      <input type="button" class="refer-now-btn" value="refer now" name="refernow" id="refernow" onClick="showAndroidToast('refer');" />
    </div>
  </form>

  <form method="post" name="refer_now_footer">
    <div class="refer-now-button">
      <!-- <button type="submit" class="refer-now-btn" id="refer_now_footer">refer now</button> -->
       <input type="button" class="refer-now-btn" value="refer now" name="refer-footer" id="refer-footer" onClick="showAndroidToast('bottom_refer');" />
    </div>
</form>

以下是我的 java 代码;

   mWebview.loadUrl(link);

            mWebview.setWebChromeClient(new WebChromeClient() {
                @Override
                public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                    //Required functionality here
                    return super.onJsAlert(view, url, message, result);
                }});
            mWebview.addJavascriptInterface(new WebAppInterface(this), "AndroidInterface");
            mWebview.getSettings().setJavaScriptEnabled(true);
            mWebview.getSettings().setLoadsImagesAutomatically(true);
            mWebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    public class WebAppInterface {
    Context mContext;

    // Instantiate the interface and set the context
    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void showevent(String toast) {
        if(toast.equalsIgnoreCase("refer")){
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }else if(toast.equalsIgnoreCase("bottom_refer")){
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }
}

当我运行此代码时;这给了我以下错误:

"Uncaught ReferenceError: AndroidInterface is not defined",

3个回答

要在 Android 中从 Web 视图执行按钮点击:

HTML 和 Java 脚本

<!DOCTYPE html>
<html lang="en">
<body>
<input type="button"  value="Say hello" onClick="showAndroidToast('OK')" />
<input type="button"  value="hello mr.vicky" onClick="showAndroidToast('MR.VICKY')" />
<br/><br/>

<script type="text/javascript">
        <!-- Sending value to Android -->
        function showAndroidToast(toast) {
            AndroidInterface.showToast(toast);
        }
      </script>
</body>
</html>

JAVA 代码

package com.ellvar.Activity.Common.Activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;

import com.ellvar.CustomView.CustomDialog;
import com.ellvar.CustomView.CustomProgressDialog;
import com.ellvar.R;
import com.ellvar.Utils.Utility;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TermsConditionActivitydemo extends AppCompatActivity {

    @BindView(R.id.wv_termscondition)
    WebView wvTermscondition;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    CustomProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_terms_condition);
        ButterKnife.bind(this);
        if (!Utility.getInstance().isNetworkAvailable(this)) {
            CustomDialog.getInstance().showalert(this, getString(R.string.please_check_internet_connection));
        }else{
            loaddata();
        }
    }
    private void loaddata() {
        wvTermscondition.loadUrl("file:///android_asset/index.html");
        wvTermscondition.setWebChromeClient(new WebChromeClient() {
                                  @Override
                                  public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                                      //Required functionality here
                                      return super.onJsAlert(view, url, message, result);
                                  }});
        wvTermscondition.addJavascriptInterface(new WebAppInterface(this), "AndroidInterface");
        wvTermscondition.getSettings().setJavaScriptEnabled(true);
        wvTermscondition.getSettings().setLoadsImagesAutomatically(true);
        wvTermscondition.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }


    public class WebAppInterface {
        Context mContext;

        // Instantiate the interface and set the context
        WebAppInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void showToast(String toast) {
            if(toast.equalsIgnoreCase("OK")){
                Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
            }else if(toast.equalsIgnoreCase("MR.VICKY")){
                Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
            }
        }
    }


}

输出

在此处输入图像描述

Mr.vicky patel
2019-04-11

我不确定您是否可以使用不同的 names 多次调用 addJavaScriptInterface 方法。请尝试将您的 performClick1 方法移至同一 JavaScriptInterface 下,例如:

    view.addJavascriptInterface(new Object() {
        @JavascriptInterface
        public void performClick() {
            Intent i = new Intent(FollowPageActivity.this, MyLoyaltyActivity.class);
            startActivity(i);
        }

        @JavascriptInterface
        public void performClick1() {
            Intent i = new Intent(FollowPageActivity.this, MyLoyaltyActivity.class);
            startActivity(i);
        }
    }, "refer_now");// identify which button you click
Uma Sankar
2019-04-10

名为 refer_now 的表单将被分配给全局对象 window 的同一属性。

这意味着 onclick="refer_now.performClick();" 中的 refer_now 是表单元素。

关键是确保 form 的名称和 javascriptinterface 名称不同。

这里有一个解决方案:保留 java 代码并将 html 更改为以下内容:

  <form method="post" name="refer_now_form">
    <div class="refer-now-button">
      <!-- <button type="submit" class="refer-now-btn" id="refer-now">refer now</button> -->
      <input type="submit" class="refer-now-btn" value="refer now" name="refernow" id="refernow" onclick="refer_now.performClick();" />
    </div>
  </form>

 <form method="post" name="refer_now_footer_form">
    <div class="refer-now-button">
      <!-- <button type="submit" class="refer-now-btn" id="refer_now_footer">refer now</button> -->
       <input type="submit" class="refer-now-btn" value="refer now" name="refer-footer" id="refer-footer" onclick="refer_now_footer.performClick1();" />
    </div>
</form>
edvard chen
2019-04-10