简体   繁体   English

从文本框中获取值-Android WebView和JavascriptInterface问题

[英]Getting values from textbox - Android WebView and JavascriptInterface issue

I'm trying to get simple values from two text boxes from my website on click of a button present there. 我试图通过单击网站上存在的按钮从网站的两个文本框中获取简单值。

I followed this - https://developer.android.com/guide/webapps/webview.html#BindingJavaScript 我遵循了这个-https://developer.android.com/guide/webapps/webview.html#BindingJavaScript

to basically bind my client-side Android code to my JavaScript function on the website. 基本上将我的客户端Android代码绑定到网站上的JavaScript函数。

This is my Android code - 这是我的Android代码-

package course.org.webviewtesting;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Created by User on 12/29/2016.
 */

public class WebViewTest extends Activity {

WebView mWebView;
ImageView logo;

@Override
public void onCreate(Bundle savedInstanceState) {

    final Activity mActivity = this;

    CookieSyncManager.createInstance(mActivity);
    CookieSyncManager.createInstance(getApplicationContext());
    CookieSyncManager.getInstance().sync();
    CookieManager.getInstance().setAcceptCookie(true);

    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    logo = (ImageView) findViewById(R.id.logo);

    logo.setVisibility(View.VISIBLE); //logo as a loading screen
    // Makes Progress bar Visible
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);



    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.addJavascriptInterface(new WebAppInterface(this), "android");
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("myURLhere");
    mWebView.setWebChromeClient(new WebChromeClient() {


        public void onProgressChanged(WebView view, int progress) {
            //Make the bar disappear after URL is loaded, and changes string to Loading...
            mActivity.setTitle("Loading...");
            mActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

            if (progress == 100) {
                mActivity.setTitle(R.string.app_name);
                logo.setVisibility(View.GONE);
            }
        }


    });

    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
        System.out.println(" url is " + url);

        }
    });

}

// this is from the Android Docs
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 showFields(String email, String name) {
        Toast.makeText(mContext, email+" "+name , Toast.LENGTH_SHORT).show();
        System.out.println("Email " + email + "Name - "+name);
    }
}

}

My HTML is as follows : 我的HTML如下:

<input type="email" id="loginEmail" name="loginEmail" ng-model="loginEmail" ng-required="true" />

<input type="name" id="name" name="name" ng-model="name" ng-required="true" />

<button type="submit" class="btn btn-large" ng-click="showValues();" onClick="showValuesAndroid()">Check Values</button>

And the corresponding JavaScript : 和相应的JavaScript:

<script type="text/javascript">
function showValuesAndroid() {
var a = document.getElementById("loginEmail").value;
var b = document.getElementById("name").value;
Android.showFields(a,b);
};
    </script>

My function gets the values in variables a and b, but I just can't seem to get the toast message on my WebView on click of the 'Check Values' button. 我的函数获取变量a和b中的值,但是单击“检查值”按钮后,我似乎无法在WebView上获得吐司消息。 Also - a System.out.println doesn't seem to be showing any values either. 另外-System.out.println似乎也不显示任何值。

Been stuck at this since almost 2 hours, what am I missing out/ doing wrong ?? 自将近2个小时以来一直处于这种状态,我错过了什么/做错了什么?

The documentation is wrong, once JavaScript is case sensitive. 一旦JavaScript区分大小写,该文档是错误的。 If you pay attention to your Logcat, you'd probably see a message like: 如果您关注Logcat,则可能会看到类似以下的消息:

com.androidbuts.sample I/chromium: [INFO:CONSOLE(XX)] "Uncaught ReferenceError: Android is not defined", source: ...

That's because the Android object is not properly set . 这是因为未正确设置Android对象

As @Jonas w stated, change the addJavascriptInterface() config to: 如@Jonas w所述,将addJavascriptInterface()配置更改为:

mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

... and it'll work fine! ...它将正常工作! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM