uni-app @focus事件中使用uni.hideKeyboard(),导致其他没有@focus事件的input错误触发该事件

uni-app @focus事件中使用uni.hideKeyboard(),导致其他没有@focus事件的input错误触发该事件

类别 信息
产品分类 uniapp/App
PC开发环境 Windows
PC版本号 版本 10.0.18363.1256
HBuilderX类型 正式
HBuilderX版本 3.0.7
手机系统 Android
手机版本号 Android 10
手机厂商 华为
手机机型 YAL-AL00
页面类型 vue
打包方式 云端
项目创建方式 HBuilderX

示例代码:

<template>  
    <view class="content">  
        <input type="text" placeholder="A" [@focus](/user/focus)="testF()" />  
        <input type="text" placeholder="B" />  
    </view>  
</template>  

<script>  
    export default {  
        methods: {  
            /**  
             * A点击  
             */  
            testF() {  
                console.log('获取焦点 ' + new Date())  
                // #ifdef APP-PLUS  
                uni.hideKeyboard()  
                // #endif  
            }  
        }  
    }  
</script>  

<style>  
    .content {  
        display: flex;  
        flex-direction: column;  
        align-items: center;  
        justify-content: center;  
    }  
</style>

操作步骤:

  1. 点击A input,软键盘隐藏

  2. 点击B input,软键盘隐藏,触发A焦点事件

  3. 点击B input,软件盘正常

  4. 点击空白,焦点消失,软件盘隐藏

  5. 点击A input,软键盘隐藏

  6. 点击B input,软键盘隐藏,触发A焦点事件

预期结果:

点击A input,软键盘隐藏
点击B input,软键盘正常显示

实际结果:

点击A input,软键盘隐藏
点击B input,软键盘隐藏


更多关于uni-app @focus事件中使用uni.hideKeyboard(),导致其他没有@focus事件的input错误触发该事件的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复

问题排查中,已加分,感谢您的反馈!

更多关于uni-app @focus事件中使用uni.hideKeyboard(),导致其他没有@focus事件的input错误触发该事件的实战教程也可以访问 https://www.itying.com/category-93-b0.html


HBuilderX 3.1.3 alpha 已修复

我用的这个版本3.1.8.20210406,还是有这个现象:其它没有用@focus="testF()"的input也隐藏软键盘了

回复 c***@126.com: 详细描述一下

好的,谢谢

我的 HBuilderX 3.1.13 /**

隐藏键盘 */ // #ifdef APP-PLUS uni.hideKeyboard() // #endif 为什么这样写之后安卓打包APK ,在app里面input 输入框还是无法 隐藏键盘??

这是一个典型的焦点事件处理问题。在uni-app中,当调用uni.hideKeyboard()时,会导致当前获得焦点的输入框失去焦点,从而触发其他输入框的@focus事件。

解决方案是使用@tap替代@focus来处理点击事件:

<input type="text" placeholder="A" @tap="testF()" />

或者使用@focus时增加判断条件:

testF(e) {
    if(e.target === this.$refs.inputA) {
        console.log('A获取焦点');
        uni.hideKeyboard();
    }
}
回到顶部