从Android WebView中获取输入值。

huangapple go评论85阅读模式
英文:

Get value from input in android webview

问题

我尝试将输入类型的值转换为字符串并存储在我的Android WebView中,但我找不到方法。以下是我的HTML代码。

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

我想获取该值并将其存储到我的Android WebView中的一个字符串中。有什么线索吗?

英文:

So I tried to get the value of an input type into a string in my android webview. But I couldnt find a way. Below is my code in the html.

&lt;input class=&quot;input100&quot;  type=&quot;hidden&quot; id=&quot;captcha1&quot; name=&quot;captcha1&quot; value=&quot;&lt;?php echo $_SESSION[&#39;captcha&#39;]; ?&gt;&quot;&gt;

And i wanted to get the value and store it into a string in my android webview. Any clue??

答案1

得分: 1

你需要在WebView中注册JavaScript接口。您可以使用注解@JavascriptInterface向Android WebView控制器添加方法,这些方法可以从WebView控制器中调用。还要不要忘记从proguard中移除它们...

获取更多信息请参考 https://stackoverflow.com/questions/3298597/how-to-get-return-value-from-javascript-in-webview-of-android

这个示例将从HTML文本中读取数据并放入Android的TextView中。

在向WebView的JavaScript公开Android方法时,您应该始终非常小心。

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 {
        "&lt;!DOCTYPE html&gt;&lt;head&gt;&lt;title&gt;Loading...&lt;/title&gt;&lt;/head&gt;&lt;body&gt;" +
        "&lt;input  type=\"text\" id=\"captcha1\" name=\"captcha1\" onkeyup=\"fromAndroid.getData(value);\"&gt;" +
        "&lt;img src=\"/&gt;&lt;/body&gt;&lt;/html&gt;"
    }

    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 have to register javascript interface to webview. You can add methods with annotaion @JavascriptInterface to android webview controller that can be called from Webview controll. Also don't forgate to remove those from proguard...
for more info https://stackoverflow.com/questions/3298597/how-to-get-return-value-from-javascript-in-webview-of-android
This example will read from html text and put in android text view.
You should be always carefullwhile exposing android method to webview java script

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 =&quot;&quot;
val myWebView = WebView(context)
val html: String by lazy{&quot;&lt;!DOCTYPE html&gt;&lt;head&gt;&lt;title&gt;Loading...&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&quot;+
        &quot;&lt;input  type=\&quot;text\&quot; id=\&quot;captcha1\&quot; name=\&quot;captcha1\&quot; onkeyup=\&quot;fromAndroid.getData(value);\&quot;&gt;&quot;+
        &quot;&lt;img src= /&gt;&lt;/body&gt;&lt;/html&gt;&quot;}

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, &quot;fromAndroid&quot;)
    myWebView.loadDataWithBaseURL(&quot;&quot;, html, &quot;text/html&quot;,&quot;UTF-8&quot;, null);
}
companion object{
    const val TAG = &quot;MyWebView&quot;
}
}

interface JavaScriptInterface{
  fun getData(data: String)
}

答案2

得分: 0

以下是翻译好的部分:

override fun onCreate(savedInstanceState: Bundle?) {
    // 从布局 ID 引用的 webView
    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() 应该与你在 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
    }
}

希望这对你有帮助。如果有其他问题,请随时提出。

英文:

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:

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

        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
        }
    }

huangapple
  • 本文由 发表于 2020年7月30日 17:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63170016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定