在启动应用程序时,我需要将键盘显示在屏幕上。
我可以在iOS中实现这一点,在那里我将焦点设置在文本字段上,键盘就会出现。然而,在Android和BlackBerry中,即使文本字段越来越集中,键盘也不会显示出来。
我使用了以下代码来设置焦点:
setTimeout(function(){
document.getElementById("mglTxtEmailAddr").focus();
}, 200);
Android假设您使用的是Worklight 6.2或更高版本,则可以使用以下代码来显示Android键盘的焦点。
Javascript:
document.getElementById('mglTxtEmailAddr').addEventListener('focus', function(){
WL.App.sendActionToNative('showKeyboard',{});
});
setTimeout(function(){
document.getElementById("mglTxtEmailAddr").focus();
}, 200);
在Worklight Studio生成的活动类中,在onCreate函数的末尾添加以下操作接收器。
YourActivity.java:
WL.getInstance().addActionReceiver(new WLActionReceiver(){
@Override
public void onActionReceived(String action, JSONObject arg1) {
if("showKeyboard".equals(action)) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
}
});
iOS
对于AppDelegate类中的iOS,只需在wlInitDidCompleteSuccessfully
中添加以下代码:
[cordovaViewController.webView setKeyboardDisplayRequiresUserAction:NO];