【uniapp】关于小程序输入框聚焦、失焦(输入法占位)的问题

聊天小程序,界面带有输入框,当输入框中聚焦后,底部自动谈起输入法。此时输入框也要随之出现在输入法上方。默认情况下,输入框此时会被输入法覆盖掉。

以下是亲自实践,解决这个问题的方法:

一、小程序大概布局

复制代码
<template>
	<view style="height: 100%; display: flex; flex-direction: column;">
		
		<!-- 自定义导航栏 -->
		<head-nav :navRedirectPage="navRedirectPage"></head-nav>
		
		<!-- 小程序界面内容 -->
		<view class="container" :style="containerStyle">
			<!-- 聊天界面内容 -->
			<scroll-view ref='list' :scroll-top="scrollTop" class="chat-container" :scroll-y="true"></scroll-view>
			
			<!-- 底部输入框 -->
			<view class="bottom-bar"  :style="bottom_autosize_style">
				<view class="input-view">
						<uv-textarea selectable="true" :cursor-spacing="20" v-model="inputText" :inputBorder="false" customStyle="overflow: scroll;max-height: 75px;border-width: 0px !important;background-color: #F7F7FC;margin-left: 12px;padding: 7px;"
			:adjust-position="false" :show-confirm-bar='false' @blur="inputBindBlur" :maxlength="500" autoHeight placeholder="请输入内容" @focus="inputBindFocus"></uv-textarea>					
					<button class="send-button" @click="sendMessage"
						:disabled="!inputText.trim().length>0 || sendDisabled">
						<image class="send-message" src="/static/v2/aichat/fasong.png" />
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

css样式表:

  • 默认样式为:

    复制代码
      // 聊天窗口
      .chat-container {
      	flex-grow: 1; // 窗口大小为可变
      	overflow-y: auto;
      }
    
      // 底部输入框
      .bottom-bar {
      		position: relative;
      		display: flex;
      		align-items: center;
      		padding: 6px 13px 6px 13px;  // 主要是padding-bottom变化
      }
  • 当焦点在输入框内时,需要调整内容:

    // 聚焦方法
    inputBindFocus(e){
    console.log('e.detail.height:', e.detail.height)
    if (e.detail.height) {
    this.bottom_autosize_style = "padding-bottom:" + (e.detail.height + 6ssss) + "px;"
    if (!this.showScrollToBottom) {
    this.scrollToBottom();
    }
    }
    },
    // 失焦方法
    inputBindBlur(){
    // this.containerStyle = ' height:' + this.containerHeight + 'px;';
    this.bottom_autosize_style = ""
    if (!this.showScrollToBottom) {
    this.scrollToBottom();
    }
    }

请注意 bottom_autosize_style 这个变量的变化,即可实现。

相关推荐
2501_9159214327 分钟前
iOS混淆与IPA加固全流程(iOS混淆 IPA加固 Ipa Guard实战)
android·ios·小程序·https·uni-app·iphone·webview
liusheng29 分钟前
腾讯地图 SDK 接入到 uniapp 的多端解决方案
前端·uni-app
游戏开发爱好者844 分钟前
iOS 26 App 开发阶段性能优化 从多工具协作到数据驱动的实战体系
android·ios·小程序·uni-app·iphone·webview·1024程序员节
2501_915106321 小时前
深入剖析 iOS 26 系统流畅度,多工具协同监控与性能优化实践
android·ios·性能优化·小程序·uni-app·cocoa·iphone
游戏开发爱好者84 小时前
iOS 26 App 查看电池寿命技巧,多工具组合实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
xiaohe06014 小时前
🚀 拥抱 create-uni,一行命令轻松集成 Uni ECharts!
vue.js·uni-app·echarts
abigale036 小时前
开发实战 - ego商城 - 补充:小程序性能优化
小程序·uniapp·小程序分包
说私域7 小时前
开源AI智能客服、AI智能名片与S2B2C商城小程序融合下的商家客服能力提升策略研究
人工智能·小程序
游戏开发爱好者812 小时前
iOS 混淆工具链实战 多工具组合完成 IPA 混淆与加固 无源码混淆
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088918 小时前
用多工具组合把 iOS 混淆做成可复用的工程能力(iOS混淆|IPA加固|无源码混淆|Ipa Guard|Swift Shield)
android·开发语言·ios·小程序·uni-app·iphone·swift