我在表单中有一个 WebView
。我正在使用这段代码。
WebView ww = (WebView)findViewById(R.id.webView);
WebSettings webSettings = ww.getSettings();
webSettings.setJavaScriptEnabled(true);
ww.loadUrl("http://www.youtube.com");
我想要的是,当页面加载完成后,我希望在搜索输入框中设置我的 TextView
值并开始搜索。
首先我认为这段代码可以工作。
webView.loadUrl("javascript: {document.getElementsByID('masthead-search-term').value ='TEST1';};");
但是当我使用这段代码时,我在所有页面中只看到“TEST1”。
感谢现在的帮助!
最佳答案
我假设 textView1
是您查询所在的 TextView
。
试试下面的代码,
// Load the query to the search input
String query = textView1.getText().toString();
webView.loadUrl("javascript: {document.getElementById('masthead-search-term').value ='" + query + "';};");
// For the click
webView.loadUrl("javascript: {document.getElementById('search-btn').click();};");
关于javascript - Android - 将javascript代码发送到WebView的内页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39815145/