英文:
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.
<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. Any clue??
答案1
得分: 1
你需要在WebView中注册JavaScript接口。您可以使用注解@JavascriptInterface向Android WebView控制器添加方法,这些方法可以从WebView控制器中调用。还要不要忘记从proguard中移除它们...
这个示例将从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 {
"<!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 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 =""
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)
}
答案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("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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论