简体   繁体   English

从 android webview 中的输入获取值

[英]Get value from input in android webview

So I tried to get the value of an input type into a string in my android webview.因此,我尝试将输入类型的值转换为 android webview 中的字符串。 But I couldnt find a way.但我找不到办法。 Below is my code in the html.下面是我在 html 中的代码。

<input class="input100"  type="hidden" id="captcha1" name="captcha1" value="<?php echo $_SESSION['captcha']; ?>">

And i wanted to get the value and store it into a string in my android webview.我想获取该值并将其存储到我的 android webview 中的字符串中。 Any clue??有什么线索吗??

You have to register javascript interface to webview.您必须将 javascript 接口注册到 webview。 You can add methods with annotaion @JavascriptInterface to android webview controller that can be called from Webview controll.您可以将带有注释 @JavascriptInterface 的方法添加到 android webview controller 可以从 Z0B963A4F51FBBA4 控件调用的方法。 Also don't forgate to remove those from proguard... for more info How to get return value from javascript in WebView of Android?也不要伪造从 proguard 中删除这些... 了解更多信息如何从 Android 的 WebView 中的 javascript 获取返回值? This example will read from html text and put in android text view.此示例将从 html 文本读取并放入 android 文本视图。 You should be always carefullwhile exposing android method to webview java script将 android 方法暴露给 webview java 脚本时,您应该始终小心

import android.content.Context
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.widget.TextView

class MyWebView(context:Context, textView: TextView) {
var outValue:String =""
val myWebView = WebView(context)
val html: String by lazy{"<!DOCTYPE html><head><title>Loading...</title></head><body>"+
        "<input  type=\"text\" id=\"captcha1\" name=\"captcha1\" onkeyup=\"fromAndroid.getData(value);\">"+
        "<img src= /></body></html>"}

init {
    val javaScriptInterface = object: JavaScriptInterface{
        @JavascriptInterface
        override fun getData(data: String){
            outValue = data
            textView.text = data
            Log.d(TAG, data)
        }
    }
    myWebView.settings.setJavaScriptEnabled(true)
    myWebView.addJavascriptInterface(javaScriptInterface, "fromAndroid")
    myWebView.loadDataWithBaseURL("", html, "text/html","UTF-8", null);
}
companion object{
    const val TAG = "MyWebView"
}
}

interface JavaScriptInterface{
  fun getData(data: String)
}

You can use something similar in your code, The function from javascript must return the value and then it will be caught here as follows:您可以在代码中使用类似的东西,来自 javascript 的 function 必须返回该值,然后它将被捕获如下:

override fun onCreate(savedInstanceState: Bundle?) {
        // webView referenced from layout id
        webView.settings.javaScriptEnabled = true
        webView.webViewClient = MyWebViewClient()
        webView.setWebChromeClient(MyWebChromeClient())
        webView.loadUrl("http://foobar.com")
    }
    
    private class MyWebViewClient : WebViewClient() {
        override fun onPageFinished(view: WebView, url: String) {
            //functionToReturnSomething() should be \ same as you mention in javascript
            view.loadUrl("javascript:alert(functionToReturnSomething())")
        }

        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            return false
        }
    }
    
    private class MyWebChromeClient : WebChromeClient() {
        override fun onJsAlert(view: WebView, url: String, message: String, result: JsResult): Boolean {
            result.confirm()
            return true
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM