Javascript 在android和webview之间传递多个数据
我对安卓和重用IOS中使用的javascript代码比较陌生 该代码要求从Android传递数据。到目前为止,我看到的教程只传递了一条toast消息或一个简单的字符串 我将如何从Android传递数据,在那里我可以从javascript访问数据,类似于如何访问Javascript 在android和webview之间传递多个数据,javascript,android,kotlin,android-webview,Javascript,Android,Kotlin,Android Webview,我对安卓和重用IOS中使用的javascript代码比较陌生 该代码要求从Android传递数据。到目前为止,我看到的教程只传递了一条toast消息或一个简单的字符串 我将如何从Android传递数据,在那里我可以从javascript访问数据,类似于如何访问数据。问题或数据。选项正在被访问 另外,下面的javascript在passData()函数中将用户选择的答案返回给Android。有人能提供一个示例代码,告诉我如何接受和处理Android的返回值吗?我正在使用Kotlin <scr
数据。问题或数据。选项正在被访问
另外,下面的javascript在passData()
函数中将用户选择的答案返回给Android。有人能提供一个示例代码,告诉我如何接受和处理Android的返回值吗?我正在使用Kotlin
<script type="text/javascript">
function printData(data) {
var questionLabel = document.getElementById("question")
questionLabel.innerHTML = data.question
var counterLabel = document.getElementById("counter")
counterLabel.innerHTML = "Question " + data.activityCounter + " of " + data.numberOfActivities
var im = document.createElement('img');
if (data.graphicURI != null) {
im.src = data.graphicURI;
im.className = 'img-fluid img-rounded image-question';
document.getElementById('imageContainer').appendChild(im)
}
var i=0, doc = document, docFrag = document.createDocumentFragment();
for (; i < data.choices.length ; i++){
var elem = doc.createElement('input');
elem.type = 'button';
elem.className = 'card mb-3 col-10 card-body';
elem.value = data.choices[i];
elem.tag = i;
elem.addEventListener("click", function() {
passData(this.value)
}, false);
docFrag.appendChild(elem);
}
document.getElementById("choicesContainer").appendChild(docFrag);
if (((window.innerHeight + window.scrollY) >= document.body.offsetHeight) == false) {
document.getElementById("seeBottom").style.display = "block";
}
}
function passData(data) {
if (confirm("Are you sure you want to submit?")) {
console.log("yes");
window.webkit.messageHandlers.answer.postMessage(data);
} else {
console.log("nO");
}
}
</script>
函数printData(数据){
var questionLabel=document.getElementById(“问题”)
questionLabel.innerHTML=data.question
var counterLabel=document.getElementById(“计数器”)
counterLabel.innerHTML=“Question”+data.numberOfActivities的+data.activityCounter+”
var im=document.createElement('img');
if(data.graphicURI!=null){
im.src=data.graphicURI;
im.className='img流体img圆形图像问题';
document.getElementById('imageContainer').appendChild(im)
}
var i=0,doc=document,docFrag=document.createDocumentFragment();
对于(;i=document.body.offsetHeight)==false){
document.getElementById(“seeboth”).style.display=“block”;
}
}
函数passData(数据){
如果(确认(“您确定要提交吗?”)){
控制台日志(“是”);
window.webkit.messageHandlers.answer.postMessage(数据);
}否则{
控制台日志(“否”);
}
}
首先,我们需要在javascript代码和客户端android代码之间创建一个接口
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 sendValueToAndroid(int value) {
// Do your operations on value here
// For example:
value = value * 500;
Toast.makeText(getContext(),"Value is :" + value , Toast.LENGTH_SHORT).show();
}
}
注意:不要忘记在函数上方添加@JavascriptInterface
注释,否则JS将无法识别该函数
然后我们可以将这个接口绑定到webview,比如
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
现在android代码和javascript之间的接口已经设置好了。我们可以将数据从javascript共享到android
为了访问javascript中的函数,可以这样调用函数
Android.sendValueToAndroid(5);
这里,Android是我们用来表示JS的标识符,这是Android端的一个函数
因此,为了有其他类似的函数,将它们添加到WebAppInterface类中,您可以在JS端访问该函数,如
Android.yourFunction();