学习笔记——uni-app在APP端input标签聚焦同时不显示软键盘

本文介绍如何在uni-app中实现扫码功能时控制软键盘的显示与隐藏。通过设置input组件的auto-blur属性并配合使用额外的image组件控制,可以在聚焦文本框时使软键盘自动隐藏。
部署运行你感兴趣的模型镜像

在使用uni-app完成扫码功能时,有时需要聚焦文本框的同时,需要软键盘消失。正常情况下,聚焦文本框会弹出软键盘,而软键盘有时会过大覆盖很多内容,或者将页面其他内容顶上去。

方法:

这里用到了input组件一个重要的属性auto-blur

<input type="text" class="scan-input" placeholder="扫描新的二维码添加物料" 
       @confirm="addCode" autocomplete="off" @focus="changeFocus" @blur="changeBlur"         
       :value="inputText" :auto-blur="false"/>
<image src="../../static/images/unselected_keyboard.png" v-if="isHideKeyboard"         
       @tap="changeKeyboard" class="keyboard-icon"></image>
<image src="../../static/images/selected_keyboard.png" v-if="!isHideKeyboard"         
       @tap="changeKeyboard" class="keyboard-icon"></image>

其中image组件使用来控制在聚焦文本框时键盘是否需要显示。

默认是不需要显示的,也就是说在App上你点击文本框聚焦了,软键盘会出现再消失,以后你点击文本框后都不会再次出现软键盘。这样,你就可以扫码,并回车键执行操作。

当你点击一次image组件时,表示需要软键盘。当你再次聚焦文本框后,会弹出软键盘,这时你可以使用软键盘输入

export default {
    data() {
        return {
            inputText: '',
            isHideKeyboard: true
        }
    },
    methods: {
        // #ifdef APP-PLUS
		    changeFocus() {
				if(this.isHideKeyboard) uni.hideKeyboard()
			},
		// #endif
        changeKeyboard() {
			this.isHideKeyboard = !this.isHideKeyboard
			if(this.isHideKeyboard) uni.hideKeyboard()
		},
        // 扫码
        addCode(e) {
		    this.inputText = e.detail.value
			if(!this.inputText) {
				uni.showToast({
					title: '请输入二维码',
					icon: 'none'
				})
				return
			}
			if(this.isScaning) {
				this.inputText = ''
				return
			}
			let innerAudioContext = uni.createInnerAudioContext()
			innerAudioContext.volume = 1
			this.isScaning = true
			uni.request({
				url: this.websiteUrl + '/bom/autoScanCode',
				method: 'POST',
				header: {
					"Content-type": "application/x-www-form-urlencoded",
					withCredentials: true
				},
				data: {
					codeNumber: this.inputText,
					orderId: this.orderId,
					orderNumber: this.orderNumber
				},
				success: async res => {
					if(res.data.state == 'failed') {
						innerAudioContext.src = '/static/sound/fail.mp3'
						innerAudioContext.play()
						uni.showToast({ title: res.data.msg, icon: 'none' })
						return
					}
					innerAudioContext.src = '/static/sound/success.mp3'
					innerAudioContext.play()
					await this._getProductionList()
					this._getList(this.activeId)
				},
				complete: res => {
					this.inputText = ''
					this.isScaning = false
					innerAudioContext.autoplay = true
					setTimeout(() => { innerAudioContext.destroy() }, 500)
				}
			})
		},
    }
}

以上代码就可以实现聚焦文本框时不会弹出软键盘。

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一一GG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值