以编程方式将文本插入Webview的文本字段中

问题描述 投票:1回答:1

我有一个Web视图,我想在其中插入一些文本(例如自动填充功能)。有什么办法吗?我尝试使用Java脚本。

document.execCommand('insertHTML', false,text );

但是没有用。

java android android-webview
1个回答
1
投票

您需要使用android提供的javascript接口机制。

WebView webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.loadUrl("javascript://your_javascrpit_method");

0
投票

要在webview的输入文本字段中插入文本,您必须使用如下所示的javascript:

webView.loadUrl("javascript:(function(){l=document.getElementById('form1:txtDrname').value='" + editTextInput.getText().toString() + "';})()");

然后您可以单击Web视图内的按钮以执行搜索:

webView.loadUrl("javascript:(function(){l=document.getElementById('form1:btnSubmit');e=document.createEvent('HTMLEvents');e.initEvent('click',true,true);l.dispatchEvent(e);})()");
© www.soinside.com 2019 - 2024. All rights reserved.