将数据从java类传递到Web View html [英] Passing data from java class to Web View html
问题描述
我正在我的 webView
现在我想做的是填充来自我的java类变量的html
中的文本框,然后自动点击提交.
Now what I want to do is to fill the textbox in the html
that came from my java class variable and then automatically hit submit.
但我不知道该怎么做.
任何想法将不胜感激.
推荐答案
首先,您的 URL 似乎不可用.
First, your URL seems not available.
如果你想在安卓应用和你的网络应用/网页之间进行数据交换,你可以通过javascript来实现.
If you want to do data exchange between android app and your web app/web page you can achieve this via javascript.
以下是来自 Android 官方网站的示例:
Here is an example from Android official site:
创建一个这样的类:
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
在您的 WebView
中:
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
在您的网页中:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
如果你想向你的网页传递一些东西,只需调用相应的javascript函数:
If you wanna pass something to your webpage, just calling corresponding javascript function:
String str = "xxx";
myWebView.loadUrl("javascript:xxx('"+str+"')");
这是参考:http://developer.android.com/guide/webapps/webview.html
这篇关于将数据从java类传递到Web View html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!