本文主要包含mui失去焦点事件,mui 失去焦点,mui文本框,mui多行文本框,mui 富文本等相关知识,教程希望在学习及工作中可以帮助到您
");
</div>
在使用MUI开发APP时,经常需要让文本框获得焦点,并弹出软键盘,方便用户操作。在使用混合模式开发时,这需要调用Native.js方法。
- var Context = plus.android.importClass("android.content.Context");
- _InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");
我将该方法封装后只需调用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******
- <pre name="code" class="javascript">}(mui, window.NativeUtil={}))