angular - 移动键盘可以在 Ionic 应用程序中始终打开吗?
问题描述
在 Ionic 聊天应用程序的聊天页面中发送消息后,我想保持我的移动键盘始终打开。因为在单击发送按钮时,第一次单击时,键盘会下降,然后在第二次单击时,会发送消息。因此,这需要时间并且在 Ionic 3 应用程序中表现出滞后性。
有什么方法可以处理 Ionic 应用程序(Ionic 3/4)中的键盘问题?
解决方案
preventDefault() and stopPropagation()
是有助于保持键盘打开的主要概念。为了完成这个功能,我在聊天页面的发送按钮中添加了#sendButton。并使用 ViewChild,
@ViewChild('sendButton') private sendButton:Button;
然后,我将以下代码添加到我的 .ts 页面。并在 ngOnInit() 上调用此函数(也可以使用 ionViewDidLoad() 或 ionViewWillLoad())
keepKeyboardVisible() {
if(!!this.sendButton){
let el = this.sendButton._elementRef.nativeElement;
console.log(el);
el.addEventListener('click', (event) => {
this.stopBubble(event);
});
el.addEventListener('touchdown', (event) => {
this.stopBubble(event);
});
el.addEventListener('touchmove', (event) => {
this.stopBubble(event);
});
el.addEventListener('touchstart', (event) => {
this.stopBubble(event);
});
el.addEventListener('touchend', (event) => { //Triggered by a phone
this.stopBubble(event);
this.sendMessage();
});
}
}
stopBubble(event) {
console.log(event);
event.preventDefault();
event.stopPropagation(); //Stops event bubbling
}
推荐阅读
- spring-boot - Wiremock 请求模式与请求参数匹配
- android - 获取给定 AccessibilityNodeInfo 的滚动方向
- firebase - NoClassDefFoundError: io/opencensus/trace/Tracestate 在 StorageClient 实例化时抛出
- reactjs - 如何避免使用 useReducer([state,dispatch]) 和 useContext 进行无用的重新渲染?
- javascript - 如何向单选按钮添加被动值
- vb.net - PDFsharp 和 VB.NET Ximage.FromStream(FromStream 不是 XImage 的成员)
- html - 在表单中包含空响应
- node.js - Moongose 数组 indexOf 未完成运行
- c++ - 循环遍历链表
- asp.net - 为什么我的 ASP.NET 网页使用某些文件扩展名引发自定义 404 错误