Android 将数据从java类传递到Web View html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11752052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing data from java class to Web View html
提问by User
I'm loading below html in my webView
我在我的 html 下面加载 webView
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.
现在我想要做的是填充html
来自我的 java 类变量的文本框,然后自动点击提交。
But I don't have any idea how to do this.
但我不知道如何做到这一点。
Any thougths will be appreciated.
任何想法将不胜感激。
回答by hungr
First, your URL seems not available.
首先,您的网址似乎不可用。
If you want to do data exchange between android app and your web app/web page you can achieve this via javascript.
如果您想在 android 应用程序和您的网络应用程序/网页之间进行数据交换,您可以通过 javascript 实现这一点。
Here is an example from Android official site:
以下是来自Android官方网站的示例:
Create a class like this:
创建一个这样的类:
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();
}
}
In your WebView
:
在您的WebView
:
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
In your web page:
在您的网页中:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
If you wanna pass something to your webpage, just calling corresponding javascript function:
如果您想将某些内容传递给您的网页,只需调用相应的 javascript 函数:
String str = "xxx";
myWebView.loadUrl("javascript:xxx('"+str+"')");
Here is the Reference: http://developer.android.com/guide/webapps/webview.html
这是参考:http: //developer.android.com/guide/webapps/webview.html
回答by AlvaroSantisteban
I would add that the load of the javascript function should be done when the html is loaded. To control that, you can use the following:
我要补充一点,javascript 函数的加载应该在加载 html 时完成。要控制它,您可以使用以下方法:
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/test.html");
webview.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
webview.loadUrl("javascript:init('" + theArgumentYouWantToPass + "')");
}
});
test.html
测试.html
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
hola
adios
</body>
<script type="text/javascript">
function init(val){
// Do whatever you want with your parameter val
}
</script>
</html>
Taken from Uncaught ReferenceError: myFunction is not defined at null:1 Android exception in webview
取自Uncaught ReferenceError: myFunction is not defined at null:1 Android exception in webview
回答by xfdai
Be carefulto call javascript function like this, the str
may include single quote or other special characters.
像这样调用 javascript 函数时要小心,str
可能包含单引号或其他特殊字符。
String str = "xxx";
myWebView.loadUrl("javascript:xxx('"+str+"')");
I suggest to encode the str
in base64, and decode it on javascript side.
我建议str
在 base64 中编码,并在 javascript 端对其进行解码。
Android
String str = "xxx"; //encode in base64 String base64Str = Base64.encodeToString(str.getBytes(), Base64.NO_WRAP); myWebView.loadUrl("javascript:xxx('"+ base64Str +"')");
Javascript
function xxx(val) { //decode from base64 var str = atob(data) }
安卓
String str = "xxx"; //encode in base64 String base64Str = Base64.encodeToString(str.getBytes(), Base64.NO_WRAP); myWebView.loadUrl("javascript:xxx('"+ base64Str +"')");
Javascript
function xxx(val) { //decode from base64 var str = atob(data) }
回答by Denish Rana
Just enable DOM Storage and write var x=
to string:
只需启用 DOM Storage 并写入var x=
字符串:
webview.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);
webview.loadUrl(urlString);
webview.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
String js = "javascript:var x =document.getElementById('username').value = '"+user+"';var y=document.getElementById('password').value='"+pass+"';";
if (Build.VERSION.SDK_INT >= 19) {
view.evaluateJavascript(js, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
}
});
}
else {
view.loadUrl(js);
}
}
回答by Levon Petrosyan
Pass the paramter directly in the url
直接在url中传递参数
webView.loadUrl("file:///android_asset/animation.html?message=testing");
Get the paramter in html
file
获取html
文件中的参数
var url_string = window.location.href
var url = new URL(url_string);
var message= url.searchParams.get("message");