uniapp实现一个键盘功能

前言

因为公司需要,所以我....

演示

代码

键盘组件代码

javascript 复制代码
<template>
	<view class="keyboard_container">
		<view class="li" v-for="(item, index) in arr" :key="index" @click="changArr(item)" :style="item=='删除' ? 'width: 122rpx' : item=='空格' ? 'width: 254rpx' : ''">
			<view>{{item}}</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				arr: [
					"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 
					"a", "b", "c", "d", "e", "f", "g", "h",
					"i", "j", "k", "l", "m", "n", "o", "p", 
					"q", "r", "s", "t", "u", "v", "u", "x", "y", "z",
					"符", "大","删除"
				]
			};
		},
		
		methods: {
			changArr(text) {
				let isReturn = true
				switch(text) {
					case '返':
						this.test0()
						isReturn = false
					break
					case '符':
						this.test2()
						isReturn = false
					break
					case '大':
						this.test1()
						isReturn = false
					break
					case '小':
						this.test0()
						isReturn = false
					break
				}
				isReturn ? this.$emit("onClickText", text) : ''
			},
			test0() {
				let arr1 = [
					"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
					"a", "b", "c", "d", "e", "f", "g", "h",
					"i", "j", "k", "l", "m", "n", "o", "p", 
					"q", "r", "s", "t", "u", "v", "u", "x", "y", "z",
					"符", "大","删除"
				]
				this.arr = arr1
			},
			test1() {
				let arr1 = [
					"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
					"A", "B", "C", "D", "E", "F", "G", "H",
					"I", "J", "K", "L", "M", "N", "O", "P", 
					"Q", "R", "S", "T", "U", "V", "U", "X", "Y", "Z",
					"符", "小","删除"
				]
				this.arr = arr1
			},
			test2() {
				let arr1 = [
					"!", "''", "#", "$", "%", "&", "(", ")", "'", "*",
					"+", ",", "-", ".", "/", ":", ";", "<",
					"=",">", "?", "@", "[", "]", "\\", "^",
					"_", "`", "{", "|", "}", "~", 
					"空格", "返", "大","删除"
				]
				this.arr = arr1
			}
		}
	}
</script>

<style lang="scss" scoped>
	.keyboard_container {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		// background-color: gold;
		.li {
			font-size: 32rpx;
			width: 56rpx;
			height: 56rpx;
			line-height: 56rpx;
			margin-left: 10rpx;
			border-radius: 4rpx;
			text-align: center;
			color: #fff;
			background-color: #1890ff;
		}
		.li:active {
			background-color: #BBBBBB;
			color: #fff;
		}
		.li:first-child {
			margin-left: 0rpx;
		}
		.li:nth-child(9) {
			margin-left: 0rpx;
		}
		.li:nth-child(17) {
			margin-left: 0rpx;
		}
		.li:nth-child(25) {
			margin-left: 0rpx;
		}
		.li:nth-child(33) {
			margin-left: 0rpx;
		}
		
		.li:nth-child(n+9) {
			margin-top: 10rpx;
		}
	}
</style>

使用代码

javascript 复制代码
<template>
	<view class="buyCard_container">
		<view class="content">
			<view class="left">
				<view class="list"></view>
			</view>
			<view class="right">
				<keyboard @onClickText="clickText"></keyboard>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				studentNumber: '',
			}
		},
		onLoad() {

		},
		methods: {
			clickText(text) {
				console.log(text)
				if (text == '空格') {
					text = ' '
				}
                if (text == '删除') {
					this.studentNumber = this.studentNumber.substring(0,                 this.studentNumber.length - 1)
							return false
				}
                this.studentNumber = this.studentNumber + text
			}
		}
	}
</script>

<style lang="scss" scoped>
	
</style>
相关推荐
2501_915106328 小时前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone
dcloud_jibinbin10 小时前
【uniapp】小程序体积优化,分包异步化
前端·vue.js·webpack·性能优化·微信小程序·uni-app
2501_9160088910 小时前
金融类 App 加密加固方法,多工具组合的工程化实践(金融级别/IPA 加固/无源码落地/Ipa Guard + 流水线)
android·ios·金融·小程序·uni-app·iphone·webview
2501_9159214311 小时前
Fastlane 结合 开心上架(Appuploader)命令行版本实现跨平台上传发布 iOS App 免 Mac 自动化上架实战全解析
android·macos·ios·小程序·uni-app·自动化·iphone
游戏开发爱好者812 小时前
iOS 上架要求全解析,App Store 审核标准、开发者准备事项与开心上架(Appuploader)跨平台免 Mac 实战指南
android·macos·ios·小程序·uni-app·iphone·webview
00后程序员张13 小时前
混淆 iOS 类名与变量名的实战指南,多工具组合把混淆做成工程能力(混淆 iOS 类名变量名/IPA 成品混淆Ipa/Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074716 小时前
iOS文件管理工具深度剖析,从系统沙盒到跨平台文件操作的多工具协同实践
android·macos·ios·小程序·uni-app·cocoa·iphone
shykevin17 小时前
uni-app x开发商城系统,扩展组件uni-ui实现底部商品导航
uni-app
QuantumLeap丶17 小时前
《uni-app跨平台开发完全指南》- 05 - 基础组件使用
vue.js·微信小程序·uni-app
2501_9159184119 小时前
Flutter 加固方案对比与实战,多工具组合的跨平台安全体系(Flutter App 加固/IPA 成品混淆/Ipa Guard CLI/自动化安全流程)
安全·flutter·ios·小程序·uni-app·自动化·iphone