MUI开发APP文本框获得焦点并弹出软键盘

来源:互联网 发布:java 文件读取到缓存中 编辑:程序博客网 时间:2024/05/21 21:01

在使用MUI开发APP时,经常需要让文本框获得焦点,并弹出软键盘,方便用户操作。在使用混合模式开发时,这需要调用Native.js方法。

MUI官网展示了调用软键盘的示例:MUI官网示例。

调用软键盘操作时需要导入Java(Android)或Objective-C类对象,即:

var Context = plus.android.importClass("android.content.Context");_InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");

详情可参考:5+ App开发Native.js入门指南

我将该方法封装后只需调用NativeUtil.focusAndOpenKeyboard方法即可。但却出现了出乎意料的问题,时而在软键盘收回时页面被拉下出现偏移。


目前这个情况依然找不到解决方案,希望有解决过类似情况的博友不吝赐教。另外附上实现的代码:


(function($, owner){//***** 强制打开软键盘  Begin******var _softKeyboardwebView, _imm, _InputMethodManager, _isKeyboardInited = false;// 打开软键盘owner.initSoftKeyboard = function() {if (mui.os.ios) {_softKeyboardwebView = plus.webview.currentWebview().nativeInstanceObject();} else {_softKeyboardwebView = plus.android.currentWebview();plus.android.importClass(_softKeyboardwebView);_softKeyboardwebView.requestFocus();var Context = plus.android.importClass("android.content.Context");_InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");var main = plus.android.runtimeMainActivity();_imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);}_isKeyboardInited = true;}// 打开软键盘owner.openSoftKeyboard = function() {if (!_isKeyboardInited) {owner.initSoftKeyboard();}if (mui.os.ios) {_softKeyboardwebView.plusCallMethod({"setKeyboardDisplayRequiresUserAction": false});} else {_imm.toggleSoftInput(0, _InputMethodManager.SHOW_FORCED);}}// 控件获得焦点并弹出软键盘       // input:需要获得焦点的控件        owner.focusAndOpenKeyboard = function(input) {setTimeout(function() {input.focus();owner.openSoftKeyboard();}, 200);}//***** 强制打开软键盘  End******}(mui, window.NativeUtil={}))



0 0
原创粉丝点击