需求:使用自定义键盘,并且实现光标删除
思路:
1、需要显示光标的同时禁止弹出系统键盘:把input的readonly属性设置为false;当弹起系统键盘时添加计时器把readonly属性设置为true;(光标显示和键盘的显示不是相关联)
2、控制光标定位到文本的指定位置:当点击自定义键盘赋值给input的时候,让input聚焦,使用setSelectionRange定位光标,input聚焦之后引起系统键盘弹起,使用思路1禁用系统键盘
this.vinnumber.el.getInputElement().then(el => {
//el就是input这个元素
let timer = null;
let timer2 = null;
this.autofocus = false;
if (el.setSelectionRange) {
el.focus();
timer = setTimeout(() =>{ //使用计时器,光标定位才生效
clearTimeout(timer);
el.setSelectionRange(el.selectionStart, el.selectionEnd);
// el.setSelectionRange(select.index+1, select.index+1);
},0);
} else if (el.createTextRange) {
var range = el.createTextRange();
range.collapse(true);
range.moveEnd('character', el.selectionEnd);
range.moveStart('character', el.selectionStart);
range.select();
}
timer2 = setTimeout(() =>{
clearTimeout(timer2);
this.autofocus = true;
},0);
});
3、自定义键盘不能以弹窗的形式显示,否则会使input失焦,失去光标。