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>
相关推荐
云上凯歌1 天前
01_AI工具平台项目概述.md
人工智能·python·uni-app
郑州光合科技余经理1 天前
O2O上门预约小程序:全栈解决方案
java·大数据·开发语言·人工智能·小程序·uni-app·php
2501_916008891 天前
在不越狱前提下导出 iOS 应用文件的过程,访问应用沙盒目录,获取真实数据
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915106321 天前
Android和IOS 移动应用App图标生成与使用 Assets.car生成
android·ios·小程序·https·uni-app·iphone·webview
木子啊1 天前
UNIAPP移动端瀑布流列表,支持APP、微信小程序、H5
uni-app·瀑布流·两列排序
2501_915918411 天前
Mac 抓包软件有哪些?Charles、mitmproxy、Wireshark和Sniffmaster哪个更合适
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
iOS 抓包绕过 SSL 证书认证, HTTPS 暴力抓包、数据流分析
android·ios·小程序·https·uni-app·iphone·ssl
WeiAreYoung1 天前
uni-app xcode 制作iOS Notification Service Extension 远程推送图文原生插件
ios·uni-app·xcode
2501_915921432 天前
iOS App 电耗管理 通过系统电池记录、Xcode Instruments 与克魔(KeyMob)组合使用
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
iOS App 测试方法,Xcode、TestFlight与克魔(KeyMob)等工具组合使用
android·macos·ios·小程序·uni-app·iphone·xcode