当前位置: 首页 > news >正文

uni-app:实现文本框的自动换行

一、实现效果

文本框回车实现就会执行自动调到下一个文本框

二、 单页面使用

适用于使用页面较少的情况,在页面内部直接写入回车事件

1、定义变量-控制当前光标所在位置

  • currentFocus 是一个“状态变量”,表示 当前哪个输入框应该获得焦点

2、初始化时给一个默认值

刚进入页面,设置为0,表明光标应该在索引为0的位置,也就是会设置的第一个文本框位置

3、视图层

  • @confirm="handleEnter(1)" —— 执行回车事件,执行回车会自动调到索引为1的位置(为null表明不会跳转到下一个)

  • :focus="currentFocus === 0" —— 如果当前的变量为0,表明focus的值为true,那么对应的文本框就会自动获得焦点并弹出键盘

4、回车事件

  • 当你在 第一个输入框 按下回车(软键盘“确认”):

    • 触发 @confirm="handleEnter(1)"
    • 调用 handleEnter(1)nextIndex = 1
    • this.currentFocus = 1 ← 更新状态
  • Vue 响应式系统检测到 currentFocus 变为 1

    • 自动重新计算所有 :focus 表达式
    • 第一个输入框::focus="currentFocus === 0"false
    • 第二个输入框::focus="currentFocus === 1"true
    • 第二个输入框获得焦点,光标跳转成功!
  • 当你在 最后一个输入框 按回车:

    @confirm="handleEnter(null)"

    • nextIndex = null
    • 条件 if (nextIndex !== null ...) 不成立
    • 执行 else 分支:uni.hideKeyboard() → 收起键盘
handleEnter(nextIndex) {if (nextIndex !== null && nextIndex !== undefined) {this.currentFocus = nextIndex} else {uni.hideKeyboard()}
},

5、完整代码

<template><view><view class="item"><span>文本框1:</span><input type="text" placeholder="请输入" v-model="text1" @confirm="handleEnter(1)" :focus="currentFocus === 0"></view><view class="item"><span>文本框2:</span><input type="number" placeholder="请输入" v-model="text2" @confirm="handleEnter(2)" :focus="currentFocus === 1"></view><view class="item"><span>文本框3:</span><input type="number" placeholder="请输入" v-model="text3" @confirm="handleEnter(3)":focus="currentFocus === 2"></view><view class="item"><span>文本框4:</span><input type="number" placeholder="请输入" v-model="text4" @confirm="handleEnter(null)":focus="currentFocus === 3"></view></view>
</template><script>export default {data() {return {text1: '', //文本1text2: '', //文本2text3: '', //文本3text4: '', //文本4currentFocus: 0 // 自动换行-数字索引};},methods: {handleEnter(nextIndex) {if (nextIndex !== null && nextIndex !== undefined) {this.currentFocus = nextIndex} else {uni.hideKeyboard()}},},onLoad() {this.currentFocus = 0}};
</script><style>.item{margin-top:20px;}input{border:1px solid #7ec3ff;padding:5px 0;}
</style>

二、适合多页面使用

通过倒挂到全局方法中,满足多页面使用情况

1、全局方法

(1)全局倒挂

在全局方法中写入

import App from './App'//回车换行事件
Vue.prototype.$handleEnter = function(nextIndex) {if (nextIndex !== null && nextIndex !== undefined) {this.currentFocus = nextIndex;} else {uni.hideKeyboard();}
};// 添加 checkPermission 方法到 Vue.prototype 上,检查权限
Vue.prototype.$checkPermission = function(username) {return new Promise((resolve, reject) => {uni.request({url: getApp().globalData.position + 'Xcxuser/checkpermission',header: {"Content-Type": "application/x-www-form-urlencoded"},method: 'POST',dataType: 'json',data: {username: username,},success: res => {console.log('Server Response:', res);resolve(res);},fail: err => {console.log(err);reject(err);}});});
};// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({...App
})app.$mount()
// #endif// #ifdef VUE3 
import { createSSRApp } from 'vue'
export function createApp() {const app = createSSRApp(App)return {app}
}
// #endif

(2)全局混入

在全局方法中写入

// 全局混入
Vue.mixin({methods: {/*** 处理回车键事件,自动切换焦点或隐藏键盘* @param {number|null} nextIndex - 下一个要聚焦的输入框索引,null表示隐藏键盘*/$handleEnter(nextIndex) {if (nextIndex !== null && nextIndex !== undefined) {this.currentFocus = nextIndex;} else {uni.hideKeyboard();}}}
});

2、界面修改

直接使用全局的方法$handleEnter

<template><view><view class="item"><span>文本框1:</span><input type="text" placeholder="请输入" v-model="text1" @confirm="$handleEnter(1)":focus="currentFocus === 0"></view><view class="item"><span>文本框2:</span><input type="number" placeholder="请输入" v-model="text2" @confirm="$handleEnter(2)":focus="currentFocus === 1"></view><view class="item"><span>文本框3:</span><input type="number" placeholder="请输入" v-model="text3" @confirm="$handleEnter(3)":focus="currentFocus === 2"></view><view class="item"><span>文本框4:</span><input type="number" placeholder="请输入" v-model="text4" @confirm="$handleEnter(null)":focus="currentFocus === 3"></view></view>
</template><script>export default {data() {return {text1: '',text2: '',text3: '',text4: '',currentFocus: 0};},onLoad() {this.currentFocus = 0;}};
</script><style>.item {margin-top: 20px;}input {border: 1px solid #7ec3ff;padding: 5px 0;}
</style>

http://www.xdnf.cn/news/1338643.html

相关文章:

  • Android - 资源类型 MINE Type
  • 教育场景下禁用html5播放器拖动进度条的例子
  • 医疗信息化实战:引领医疗行业数字化转型实践
  • 华为AUTOSAR质量目标与开发实践
  • FCN网络结构讲解与Pytorch逐行讲解实现
  • Go语言中的迭代器模式与安全访问实践
  • open3d-点云函数:变换:旋转,缩放、平移,齐次变换(R,T)等
  • 开源,LangExtract-Python库用LLM从非结构化文本提取结构化信息
  • 移动应用抓包与调试实战 Charles工具在iOS和Android中的应用
  • 自然语言处理——04 注意力机制
  • 基于Spring Cloud Gateway动态路由与灰度发布方案对比与实践指导
  • 记一次 .NET 某光谱检测软件 内存暴涨分析
  • CentOS7安装部署PostgreSQL
  • 搭建FTP文件共享服务器
  • SQL中对视图的操作命令汇总
  • 【数据结构入门】排序算法:插入排序
  • 带有 Angular V14 的 Highcharts
  • 动学学深度学习03-线性神经网络
  • hadoop-3.3.6和hbase-2.4.13
  • Linux下Docker版本升级保姆攻略
  • 数据结构之排序大全(4)
  • LLaVA-3D,Video-3D LLM,VG-LLM,SPAR论文解读
  • WebSocket通信:sockjs与stomp.js的完美搭档
  • 【问题思考】为什么需要文件后缀?(gemini完成)
  • Web3 的发展挑战:技术、监管与生态的多重困境
  • 机器学习聚类算法
  • 什么是默克尔树
  • 缓存与Redis
  • C++---辗转相除法
  • 2025-08-21 Python进阶1——控制流语句