MUI - 打开页面默认弹出键盘及返回关闭键盘

打开页面默认弹出键盘及返回关闭键盘

http://www.cnblogs.com/phillyx/javascript

(function(keyboard) {
    var openSoftKeyboard = function() {
        if (mui.os.ios) {
            var webView = plus.webview.currentWebview().nativeInstanceObject();
            webView.plusCallMethod({
                "setKeyboardDisplayRequiresUserAction": false
            });
        } else {
            var Context = plus.android.importClass("android.content.Context");
            var InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");
            var main = plus.android.runtimeMainActivity();
            var imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
        }
    };
    var autoFocus = function(obj) {
        if (mui.os.ios) {
            setTimeout(function() {
                openSoftKeyboard();
                obj.focus();
                if (obj.tagName.toLowerCase() == 'textarea') {
                    moveEnd(obj);
                }
            }, 300);
        } else {
            //安卓的获取不到焦点,暂时不自动弹出
        }
    };
    keyboard.autoFocus = autoFocus;
    /**
     * 预加载页面返回时为隐藏,不会自动关闭输入法,能够经过blur来关闭
     * @param {Object} isRemoveValue
     */
    function blur(isRemoveValue) {
        var ac = document.activeElement;
        ac.blur();
        if (isRemoveValue && ac.value) {
            ac.value = '';
        }
    };
    keyboard.blur = blur;
    /**
     * @description ios下textarea.onfocus 时,默认在字首,添加该方法移动到尾部
     * @param {Object} obj
     */

    function moveEnd(obj) {
        var len = obj.value.length;
        if (document.selection) {
            var sel = obj.createTextRange();
            sel.moveStart('character', len);
            sel.collapse();
            sel.select();
        } else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
            obj.selectionStart = obj.selectionEnd = len;
        }
    };
    //          mui.plusReady(function() {
    //              if (mui.os.android) {
    //                  Array.prototype.forEach.call(document.querySelectorAll(".mui-input-group input[type='text']"), function(ip) {
    //                      ip.setAttribute('autofocus', 'autofocus');
    //                  });
    //              }
    //          });
}(window.mykeyboard = {}));
相关文章
相关标签/搜索