我试图通过单击网站上的按钮从两个文本框中获取简单的值。
我关注了这个 - https://developer.android.com/guide/webapps/webview.html#BindingJavaScript
基本上将我的客户端 Android 代码绑定(bind)到网站上的 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 似乎也没有显示任何值。
我已经被困在这个问题上近两个小时了,我错过了什么/做错了什么??
最佳答案
一旦 JavaScript 区分大小写,文档就会错误。如果您注意 Logcat,您可能会看到类似以下的消息:
com.androidbuts.sample I/chromium: [INFO:CONSOLE(XX)] "Uncaught ReferenceError: Android is not defined", source: ...
那是因为Android
object is not properly set .
正如 @Jonas w 所说,将 addJavascriptInterface()
配置更改为:
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
...它会工作得很好! :)
关于javascript - 从文本框获取值 - Android WebView 和 JavascriptInterface 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41385490/