Activity中Webview的Html按钮点击检测

Activity中Webview的Html按钮点击检测

本文介绍了Activity中Webview的Html按钮点击检测(java代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将 webview 的 html 按钮点击检测到 java 代码(活动中).
我提到了另一个 SO
通过javascript检测点击HTML按钮安卓网页视图

I was trying to detect the html button click of webview into java code(In activity) .
I referred another SO
Detect click on HTML button through javascript in Android WebView

但不起作用.我的代码:

but is not working.My code:

index.html

<html>
    <head>
        <script language="javascript">
            function js1() {
                document.loginform.method="post";
                document.loginform.action = "https://example.com/chechlogin.asp";
            }
        </script>
    </head>
    <body>
        <form name="loginform">
            <input type="text" name="empcode" value="58686" /><br/>
            <input type="password" name="emppassNTL" />
            <input type="submit" name="submit" id="submit_id" onclick="login.performClick();" />
        </form>
    </body>
</html>

MainActivity.java

package com.example.webview;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

   private EditText field;
   private WebView browser;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      field = (EditText)findViewById(R.id.urlField);
      browser = (WebView)findViewById(R.id.webView1);
      browser.getSettings().setJavaScriptEnabled(true);
      browser.setWebViewClient(new MyBrowser());
      browser.loadUrl("file:///android_asset/index.html");
   }


   @SuppressLint("JavascriptInterface")
   public void open(View view){
      String url = field.getText().toString();
      browser.getSettings().setLoadsImagesAutomatically(true);
      browser.getSettings().setJavaScriptEnabled(true);
      browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
      browser.loadUrl(url);
      browser.addJavascriptInterface(new Object()
      {
          @JavascriptInterface
        public void performClick()
        {
            Log.d("LOGIN::", "Clicked");
             Toast.makeText(MainActivity.this, "Login clicked", Toast.LENGTH_LONG).show();
        }
      }, "login");

   }
   private class MyBrowser extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
      }
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

}

但是没有调用 performClick() 方法.
请更正错误.

But the performClick() method is not being called.
Please correct the error.

推荐答案

我是这样实现的:

public class FirstActivity extends Activity {

WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    mWebView = (WebView) findViewById(R.id.webView1);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
    mWebView.loadUrl("file:///android_asset/html/File1.html");
}

public class WebAppInterface {

    Context mContext;

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

    /** Show a toast from the web page */
    @JavascriptInterface
    public void nextScreen(String pro_cat_id) {

            startActivity(new Intent(mContext,
         MainActivity.class));
    }

并且,在 html 文件中:

And, in html file:

javascript:file3.js

javascript: file3.js

function saveId(_id)
{
    localStorage.setItem("id", _id);
    Android.nextScreen(_id);
}

HTML:

<html>
    <head>
        <script type="text/javascript" src="arel/js/File3.js"></script>
    </head>
    <body>
        <button onClick="saveId('1');">1</button>
        <button onClick="saveId('2');">2</button>
    </body>
</html>

这篇关于Activity中Webview的Html按钮点击检测(java代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 19:08