机器人的WebView - 设置使用JavaScript的HTML场焦点 [英] Android WebView - Setting HTML Field focus using Javascript
问题描述
我只是一个可见的的WebView
组件,将其用来显示一些动态生成HTML应用程序(可运行JavaScript也是如此)。它启用了的WebView
。
有关几页,我想设置的输入文本框的焦点之一后,页面加载完成 - 不通过正文的onLoad()
,而是由后,页面加载完成,即在 onPageFinished一个JS调用()
。大多数的JavaScript的执行细
webview.loadURL(JavaScript的:JSMethod(););
但是,当我用同样的呼吁做了的document.getElementById('文本1')专注()
- 光标不会到达的元素,但软键盘
不会弹出。当从网页上的按钮执行同一个JavaScript code没有收到预期的效果。
我附上源$ C $ C具有 3个按键
-
焦点文本1
- 把光标放在文本1和弹出Softkeyboard -
Java的焦点文本1
- 调用Java来执行相同的JS。只能说明那里的光标,不会弹出键盘 -
Java的焦点文本1和键盘打开
- 从Java和部队键盘调用JS开
我的理解是,从浏览器中使用按钮/事件是否执行,或者从Java到 WebView.loadURL作为发送的JS执行应该是相同的()
。
下面是我的查询
- 在我缺少的东西用
按钮时,#2
?这就是我目前的code是,我只看到光标设置,但SoftKeyboard无法打开。 - 当我使用的逻辑,写在
按钮#3
,我有时看不到场上的游标,但给了我想要的效果,光标变成可见当我输入的东西在弹出的键盘。 - 可在
的行为我看到按钮#2
在Android的JS执行一个错误?或者,会不会是在的WebView
没有就是为什么只显示的光标焦点?我也试过webview.requestFocus()
- 我可以不写requestFocusOnTouch()
,因为它是唯一查看我和我期待它的自动对焦。
Java的code这演示的行为
进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.inputmethod.InputMethodManager;
进口android.webkit.JsResult;
进口android.webkit.WebChromeClient;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;
公共类WebViewProjectTestJSHTMLFocusActivity延伸活动{
活动_current = NULL;
的WebView WV = NULL;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
_current =这一点;
//setContentView(R.layout.main);
字符串数据=< HTML><身体GT; +
<脚本>功能focusser(){的document.getElementById(\文本1 \)专注();}< / SCRIPT>中+
<脚本>功能javaFocusser(){javautil.javaFocus(假);}< / SCRIPT>中+
<脚本>功能javaFocusserKeyboard(){javautil.javaFocus(真);}< / SCRIPT>中+
文本1<输入类型=文本ID =文本1/>< BR />中+
文本2';输入类型=文本ID =文本2/>< BR />中+
<输入类型=按钮值=焦点文本1的onClick ='focusser()'/>中+
<输入类型=按钮值='Java的焦点文本1的onClick ='javaFocusser()'/>中+
<输入类型=按钮值='Java的焦点文本1和键盘打开的onClick ='javaFocusserKeyboard()'/>中+
< /身体GT;< / HTML>中;
WV =新的WebView(本);
wv.getSettings()setJavaScriptEnabled(真)。
//设置一些HTML
wv.loadDataWithBaseURL(文件:/// android_asset /数据,text / html的,UTF-8,NULL);
//回拨要求后页面加载完成
wv.setWebViewClient(新CustomWebViewClient());
//启用警报电话
wv.setWebChromeClient(新CustomWebChromeClient());
//对于JS-> Java调用
wv.addJavascriptInterface(这一点,javautil);
的setContentView(WV);
}
/ **
*调用同一个javascript和强制键盘打开
* /
公共无效javaFocus(最终布尔shouldForceOpenKeyboard){
线程t =新主题(Java的focusser线){
公共无效的run(){
wv.loadUrl(JavaScript的:focusser(););
如果(shouldForceOpenKeyboard){
InputMethodManager经理=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(WV,InputMethodManager.SHOW_IMPLICIT);
}
}
};
//在查看线程上运行。
_current.runOnUiThread(T);
}
/ **
*呼吁注重方法后,页面加载完成。
* /
最后一类CustomWebViewClient
扩展WebViewClient {
@覆盖
公共无效onPageFinished(web视图查看,字符串URL){
// javaFocus(真正的);
Log.d(TestExamples,focusser呼叫完成);
}
}
最后一类CustomWebChromeClient
扩展WebChromeClient {
@覆盖
公共布尔onJsAlert(web视图查看,URL字符串,字符串消息,JsResult结果){
Log.d(TestExamples,JS提示::+消息);
返回false;
}
}
}
解决方案更新24-06-2011
为了使这项工作,你需要使用 wv.requestFocus(View.FOCUS_DOWN)
只是实际JS焦点调用之前。我修改了上面到下面的正确版本的 javaFocus()
方法。早些时候,当我提到,我使用requestFocus的(),我用,当的WebView
是在方法中初始化的onCreate()
。主要的区别是现在我们正在迫使的WebView
得到公正Javascript的的document.getElementById(文本1)之前,每次重点。重点( );
执行。
公共无效javaFocus(最终布尔shouldForceOpenKeyboard){
线程t =新主题(Java的focusser线){
公共无效的run(){
wv.requestFocus(View.FOCUS_DOWN);
wv.loadUrl(JavaScript的:focusser(););
如果(shouldForceOpenKeyboard){
InputMethodManager经理=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(WV,InputMethodManager.SHOW_IMPLICIT);
}
}
};
//在查看线程上运行。
_current.runOnUiThread(T);
}
另外要确保这个问题没有固定的,因为重点,通过触摸等引发的,我使用的是后台线程5秒的WebView的显示后启动的javaFocus()。修改后的的onCreate()
如下。
.....更多的onCreate code之前....
//启用警报电话
wv.setWebChromeClient(新CustomWebChromeClient());
//对于JS-> Java调用
wv.addJavascriptInterface(这一点,javautil);
的setContentView(WV);
新的主题(经过一段时间焦点访谈){
公共无效的run(){
尝试 {
视频下载(5000);
}赶上(InterruptedException异常E){
e.printStackTrace();
}
javaFocus(真正的);
}
}。开始();
....的onCreate()在此之后结束....
这可能是web视图没有应用的重点。尝试执行;
wv.requestFocus(View.FOCUS_DOWN);
I have an application with just a visible WebView
component to it which is used to display some dynamically generated HTML (can run Javascript too). It's enabled for the WebView
.
For a few pages I am trying to set the focus of one of the input text boxes after the page load has finished - not through Body onLoad()
, but instead by a JS call after the page has finished loading i.e. in onPageFinished()
. Most of the javascript executes fine
webview.loadURL("javascript:JSMethod();");
But when I use the same call to do a document.getElementById('text1').focus()
- the cursor does reach the element but the Soft Keyboard
won't pop out. The same javascript code when executed from a button on the page does have the desired effect.
I am attaching the source code which has 3 buttons
Focus Text1
- Put the cursor on the text1 and pops up the Softkeyboard.Java Focus Text1
- Calls Java to execute the same JS. Only shows the cursor there and doesn't pop out the keyboardJava Focus Text1 And Keyboard Open
- Calls JS from Java and Forces Keyboard open.
My understanding is that the JS execution should work the same whether executed from the browser using a button/event or as sent from Java through WebView.loadURL()
.
Here's my Queries
- Am I missing something when using
Button#2
? That's how my current code is and I only see the cursor is set but the SoftKeyboard won't open. - When I use logic as written in
Button#3
, I some times don't see the cursor on the field but gives me the desired effect and the cursor becomes visible when I type something in the popped up keyboard. - Could the behavior I see in
Button#2
be a bug in Android JS execution? Or could it be that theWebView
doesn't have focus that's why only the cursor is displayed? I also triedwebview.requestFocus()
- I can't writerequestFocusOnTouch()
as it's the only View I have and am expecting it's focused automatically.
The Java code which demos the behavior is
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewProjectTestJSHTMLFocusActivity extends Activity {
Activity _current = null;
WebView wv = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_current = this;
//setContentView(R.layout.main);
String data = "<html><body>" +
"<script>function focusser() { document.getElementById(\"text1\").focus(); } </script>" +
"<script>function javaFocusser() { javautil.javaFocus(false); } </script>" +
"<script>function javaFocusserKeyboard() { javautil.javaFocus(true); } </script>" +
"Text 1<input type='text' id='text1'/><br/>" +
"Text 2<input type='text' id='text2'/><br/>" +
"<input type='button' value='Focus Text1' onClick='focusser()'/>" +
"<input type='button' value='Java Focus Text1' onClick='javaFocusser()'/>" +
"<input type='button' value='Java Focus Text1 And Keyboard Open' onClick='javaFocusserKeyboard()'/>" +
"</body></html>";
wv = new WebView(this);
wv.getSettings().setJavaScriptEnabled(true);
// Set some HTML
wv.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8", null);
// Call back required after page load finished
wv.setWebViewClient(new CustomWebViewClient());
// Enable Alert calls
wv.setWebChromeClient(new CustomWebChromeClient());
// For JS->Java calls
wv.addJavascriptInterface(this, "javautil");
setContentView(wv);
}
/**
* Calls the same javascript and forces the keyboard to open
*/
public void javaFocus(final boolean shouldForceOpenKeyboard) {
Thread t = new Thread("Java focusser thread") {
public void run() {
wv.loadUrl("javascript:focusser();");
if(shouldForceOpenKeyboard) {
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(wv, InputMethodManager.SHOW_IMPLICIT);
}
}
};
// Run on the View Thread.
_current.runOnUiThread(t);
}
/**
* Calls focus method after the page load is complete.
*/
final class CustomWebViewClient
extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
// javaFocus(true);
Log.d("TestExamples", "focusser call complete");
}
}
final class CustomWebChromeClient
extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d("TestExamples", "JS Alert :: " + message);
return false;
}
}
}
Solution Update 24-06-2011
To make this work, you need to use wv.requestFocus(View.FOCUS_DOWN)
just before the actual JS focus call. I modified the javaFocus()
method above to the correct version below. Earlier when I mentioned that I was using requestFocus(), I was using that when the WebView
was initialized in the method onCreate()
. The primary difference is now we're forcing the WebView
to get focus each time just before the Javascript document.getElementById("text1").focus();
is executed.
public void javaFocus(final boolean shouldForceOpenKeyboard) {
Thread t = new Thread("Java focusser thread") {
public void run() {
wv.requestFocus(View.FOCUS_DOWN);
wv.loadUrl("javascript:focusser();");
if(shouldForceOpenKeyboard) {
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(wv, InputMethodManager.SHOW_IMPLICIT);
}
}
};
// Run on the View Thread.
_current.runOnUiThread(t);
}
Also to ensure that this issue wasn't fixed because of focus triggered through touch etc, I am using a background thread to initiate the javaFocus() after 5 seconds of WebView Displayed. The modified onCreate()
is below.
..... More onCreate code before....
// Enable Alert calls
wv.setWebChromeClient(new CustomWebChromeClient());
// For JS->Java calls
wv.addJavascriptInterface(this, "javautil");
setContentView(wv);
new Thread("After sometime Focus") {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
javaFocus(true);
}
}.start();
.... onCreate() ends after this....
It could be that the webview doesn't have the application focus. Try executing;
wv.requestFocus(View.FOCUS_DOWN);
这篇关于机器人的WebView - 设置使用JavaScript的HTML场焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!