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

时间:2016-12-29 18:45:40

标签: javascript java android webview

我正试图从我的网站上的两个文本框中点击该处的按钮来获取简单的值。

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

基本上将我的客户端Android代码绑定到我网站上的JavaScript函数。

这是我的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);
    }
}

}

我的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>

以及相应的JavaScript:

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

我的函数获取变量a和b中的值,但是我似乎无法在单击“检查值”按钮的WebView上获取Toast消息。 另外 - System.out.println似乎也没有显示任何值。

因为差不多2个小时就陷入困境,我错过了哪些错误?

1 个答案:

答案 0 :(得分:2)

一旦JavaScript区分大小写,文档就错了。如果您注意Logcat,您可能会看到如下消息:

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

那是因为Android object is not properly set

正如@Jonas所说,将addJavascriptInterface()配置更改为:

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

......它会正常工作! :)