Swa*_*oop 15 javascript android setfocus android-webview dom-events
我有一个只有一个可见WebView组件的应用程序,用于显示一些动态生成的HTML(也可以运行Javascript).它启用了WebView.
对于几个页面,我试图在页面加载完成后设置其中一个输入文本框的焦点 - 不是通过 Body onLoad(),而是在页面完成加载之后通过JS调用onPageFinished().大多数javascript执行正常
webview.loadURL("javascript:JSMethod();");
Run Code Online (Sandbox Code Playgroud)
但是当我使用相同的调用来做一个document.getElementById('text1').focus()- 光标确实到达元素但Soft Keyboard不会弹出.从页面上的按钮执行时,相同的javascript代码确实具有所需的效果.
我附加了有3个按钮的源代码
Focus Text1 - 将光标放在text1上并弹出软键盘.Java Focus Text1 - 调用Java来执行相同的JS.仅显示光标,不弹出键盘Java Focus Text1 And Keyboard Open - 从Java和Forces Keyboard调用JS打开.我的理解是JS执行应该是相同的,无论是使用按钮/事件从浏览器执行还是从Java发送WebView.loadURL().
这是我的查询
Button#2?这就是我当前代码的方式,我只看到光标已设置但SoftKeyboard无法打开.Button#3,我有时看不到光标在字段上,但给了我想要的效果,当我在弹出的键盘中输入内容时,光标变得可见.Button#2Android JS执行中的错误吗?或者可能是WebView没有焦点,这就是为什么只显示光标?我也尝试过webview.requestFocus()- 我不能写,requestFocusOnTouch()因为它是我唯一拥有的视图,我希望它能自动聚焦.演示行为的Java代码是
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案更新24-06-2011
要使其工作,您需要wv.requestFocus(View.FOCUS_DOWN)在实际的JS焦点调用之前使用.我将javaFocus()上面的方法修改为下面的正确版本.早些时候,当我提到我正在使用requestFocus()时,我WebView在方法中初始化时使用了它onCreate().主要区别在于,现在我们强制WebView每次在Javascript document.getElementById("text1").focus();执行之前获得焦点.
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);
}
Run Code Online (Sandbox Code Playgroud)
另外为了确保由于通过触摸等触发焦点而没有修复此问题,我使用后台线程在WebView显示5秒后启动javaFocus().修改onCreate()如下.
..... 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....
Run Code Online (Sandbox Code Playgroud)
可能是webview没有应用程序焦点.尝试执行;
wv.requestFocus(View.FOCUS_DOWN);
Run Code Online (Sandbox Code Playgroud)