打開頁面默認彈出鍵盤及返回關閉鍵盤
http://www.cnblogs.com/phillyx/
(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 = {}));
