uniapp+vue3实现数字键盘

1.首先第一步需要定义变量

let numbers = reactive([

		{
			text: '1',
			class: 'oner',
			background: '#ffffff'

		},
		{
			text: '2',
			class: 'oner',
			background: '#ffffff'
		},
		{
			text: '3',
			class: 'oner',
			background: '#ffffff'
		},
		{
			text: '4',
			class: 'oner',
			background: '#ffffff'
		},
		{
			text: '5',
			class: 'oner',
			background: '#ffffff'
		},
		{
			text: '6',
			class: 'oner',
			background: '#ffffff'
		},
		{
			text: '7',
			class: 'oner',
			background: '#ffffff'
		},
		{
			text: '8',
			class: 'oner',
			background: '#ffffff'
		},
		{
			text: '9',
			class: 'oner',
			background: '#ffffff'
		},
		{
			text: '0',
			class: 'big',
			background: '#ffffff'
		},
		{
			text: '.',
			class: 'oner',
			background: '#ffffff'
		},
	])
	// 确认的样式
	let active = reactive({
		background: ' #f2f2f2',
		color: '#d4d4d4'
	})
	let styleDelete= reactive({
		background: '#ffffff'
	})
	let stylelv = reactive({
		background: '#3bab6f'
	})
	let nums = ref('')

	let isimit = ref(false)

	let showBox = ref(false)

	let activeIndex = ref(0) //默认选中索引

	let sumindex = ref(-1) //数字键盘索引

	let valuemark = ref()
	let preinput = ref('请输入付款金额')

2.渲染页面

<view class="keyboard">
			<view class="numbers">
				<view
					style=" display: flex; flex-flow: row wrap; justify-content: flex-start; align-content: flex-start; width: 72%; height: auto;">
					<view @click="changeNums(item,index)" :class="item.class" :style="getListitemstyle(index)"
						v-for="(item,index) in numbers">
						{{item.text}}
					</view>
				</view>
				<view style="width: 28%; height: auto; display: flex; flex-flow: column wrap; ">
					<view @click="handleDelete()" class="oner" :style="styleDelete">
						<image src="../../static/chahao.png" mode="aspectFit" style="width: 50rpx; height: 50rpx;">
						</image>
					</view>
					<view class="onerplus" @click="handlePayment" :style="stylelv">
						支付
					</view>
				</view>
</view>

3.设置css样式

// 键盘样式
	.Classify {
		width: 100%;
		display: flex;
		height: 120rpx;
		margin-top: 10rpx;
		padding-left: 20rpx;
		padding-right: 30rpx;
		align-items: center;
		margin-bottom: -12rpx;
		justify-self: flex-start;

	}

	.Expenditure {
		width: auto;
		height: 60rpx;
		color: #c7c7c7;
		font-size: 30rpx;
		text-align: center;
		line-height: 60rpx;
		margin-left: 20rpx;
		border-radius: 10rpx;
		background-color: #f7f7f7;
		padding: 0rpx 20rpx 0rpx 20rpx;

	}

	.Expendituretime {
		width: auto;
		color: black;
		display: flex;
		height: 60rpx;
		font-size: 30rpx;
		line-height: 60rpx;
		margin-left: 32rpx;
		align-items: center;
		border-radius: 10rpx;
		background-color: #f7f7f7;
		justify-content: space-evenly;
		padding: 0rpx 15rpx 0rpx 20rpx;

	}

	.Expenditurelv {
		width: auto;
		height: 60rpx;
		color: #3eb575;
		font-size: 30rpx;
		text-align: center;
		line-height: 60rpx;
		margin-left: 20rpx;
		border-radius: 10rpx;
		background-color: #ebf7f1;
		padding: 0rpx 20rpx 0rpx 20rpx;

	}

	.ExpenditureH {
		width: auto;
		height: 60rpx;
		color: #f5c53a;
		font-size: 30rpx;
		text-align: center;
		line-height: 60rpx;
		margin-left: 20rpx;
		border-radius: 10rpx;
		background-color: #fdf8eb;
		padding: 0rpx 20rpx 0rpx 20rpx;

	}

	.ExpenditureL {
		width: auto;
		height: 60rpx;
		color: #8c8bc3;
		font-size: 30rpx;
		text-align: center;
		line-height: 60rpx;
		margin-left: 20rpx;
		border-radius: 10rpx;
		background-color: #f1f3f6;
		padding: 0rpx 20rpx 0rpx 20rpx;

	}

	.money-input {
		width: 100%;
		height: 150rpx;
		// padding: 0 40rpx 0 40rpx;
	}

	.Type {
		width: 100%;
		height: 154px;
		display: flex;
		margin-top: 20rpx;
		align-items: center;
		flex-flow: row wrap;
		justify-content: flex-start;
		overflow-y: auto;

	}

	.Type-box {
		display: flex;
		width: 120rpx;
		height: 140rpx;
		align-items: center;
		flex-flow: column wrap;
		justify-content: center;
		margin: 0 0rpx 10rpx 5rpx;

	}

	.Typeboxactive {
		display: flex;
		width: 120rpx;
		height: 140rpx;
		align-items: center;
		flex-flow: column wrap;
		justify-content: center;
		margin: 0 0rpx 10rpx 5rpx;
		background-color: #cbcbcb;
	}

	.inctiveclass {
		width: 80rpx;
		display: flex;
		height: 80rpx;
		font-size: 22rpx;
		border-radius: 50%;
		align-items: center;
		justify-content: center;
		background-color: #e7e7e7;

	}

	.activeclass {
		width: 80rpx;
		display: flex;
		height: 80rpx;
		border-radius: 50%;
		align-items: center;
		justify-content: center;
		background-color: #3eb575;

	}

	.keyboard {
		position: absolute;
		bottom: 0rpx;
	}

	.numbers {
		width: 100%;
		display: flex;
		height: 480rpx;
		margin-top: 10rpx;
		background-color: #fafafa;
		justify-content: flex-start;
	}

	.remark {
		width: 100%;
		color: #50648a;
		height: 100rpx;
		font-size: 32rpx;
		padding: 20rpx 0 0 30rpx;

	}

	.oner {
		display: flex;
		width: 165rpx;
		height: 100rpx;
		font-size: 34rpx;
		font-weight: bold;
		align-items: center;
		border-radius: 10rpx;
		justify-content: center;
		margin: 15rpx auto 5rpx;
		background-color: #ffffff;

	}

	.onerplus {
		display: flex;
		width: 165rpx;
		color: #ffffff;
		height: 338rpx;
		font-size: 34rpx;
		font-weight: bold;
		align-items: center;
		border-radius: 10rpx;
		justify-content: center;
		margin: 15rpx auto 5rpx;
		background-color: #9cd7b7;

	}

	.big {
		display: flex;
		width: 342rpx;
		height: 100rpx;
		font-size: 34rpx;
		font-weight: bold;
		align-items: center;
		border-radius: 10rpx;
		margin: 15rpx auto 5rpx;
		justify-content: center;
		background-color: #ffffff;

	}

	.input-container {
		position: relative;
		width: 100%;
		height: 156rpx;
		display: flex;
	}

	.input-wrapper {
		position: relative;
		width: 100%;
		height: 100%;
		// border-bottom: 1px solid #ccc;
		// padding: 0 20rpx;
		// padding: 20rpx 20rpx 0rpx 20rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}

	.input-icon {
		height: 106rpx;
		width: 5rpx;
		background-color: #cbcbcb;
		display: none;
	}

	.active {
		display: block;
		animation: blink 1s infinite; //点击后|出现
	}

	@keyframes blink {
		0% {
			opacity: 0;
		}

		50% {
			opacity: 1;
		}

		100% {
			opacity: 0;
		}
	}

	.input-iconone {
		position: absolute;
		top: 50%;
		left: 2rpx;
		transform: translateY(-50%);
		color: #666;
	}

	.input-field {
		width: 100%;
		height: 100%;
		line-height: 156rpx;
		padding-left: 80rpx;
		font-size: 80rpx;
		color: #333;
	}

	.remarks {
		width: 68px;
		height: 30px;
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}

	.box-text {
		display: flex;
		justify-content: center;
		color: #c7c7c7;
		align-items: center;
		width: 100%;
		margin-top: 10rpx;
		height: 40rpx;
		text-align: center;
		font-size: 24rpx;
	}

	.box-ative {
		display: flex;
		justify-content: center;
		color: #000000;
		align-items: center;
		width: 100%;
		margin-top: 10rpx;
		height: 40rpx;
		text-align: center;
		font-size: 24rpx;
	}

	.top {
		position: relative;
		width: 100%;
		height: 30%;
	}

	.window {
		width: 100rpx;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
	}

	.center {
		position: relative;
		width: 100%;
		height: 30%;
		display: flex;
		padding: 0 30rpx 0 30rpx;
		justify-content: center;
		align-items: center;
	}

	.bottom {
		width: 100%;
		height: 40%;
		position: relative;
	}

	.comfors {
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		margin: auto;
		position: absolute;
		width: 380rpx;
		height: 100rpx;
		border-radius: 10rpx;
		background-color: #f2f2f2;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #d4d4d4;
		border: 1rpx solid #f2f2f2;
	}

	.Record-remarks-number-of-words {
		position: absolute;
		left: 30rpx;
		bottom: -20rpx;
		color: #d4d4d4;
	}

4.这一步就到了该写方法的时候了

const comfors = () => {
		if (valuemark) {
			active.background = "#33955f"
			setTimeout(() => {
				active.background = "#3eb575"
				showremark.value = false
			}, 150)

		}
	}
	const input = (e) => {
		console.log('输入内容:', e);
		// console.log('this.index.lenth', this.value.length)
		valuemark.value = e
		if (valuemark) {
			active.background = '#3eb575'
			active.color = '#ffffff'
		} else {
			active.background = '#f2f2f2'
			active.color = '#d4d4d4'
		}
	}
	const handleDelete= () => {
		styleDelete.background = '#f7f7f7'
		setTimeout(() => {
			styleDelete.background = '#ffffff'
		}, 150)
		if (nums) {
			nums.value = nums.value.substring(0, nums.value.length - 1)
		}
	}
	const changeNums = (item, index) => {
		console.log('asdsa', item, index);
		numbers.forEach((item, idx) => {
			if (idx === index) {
				item.background = '#f7f7f7';
				setTimeout(() => {
					item.background = '#ffffff';
				}, 150)
			}
		});

		sumindex.value = index
		console.log('this.sumindex', sumindex.value)
		if (item.text == '.') {
			console.log(nums.value.indexOf(".") != -1)

			if (nums.value.indexOf(".") != -1 || nums.value.length == 0) {
				return false
			}

		}
		if (nums.value.split('.') && nums.value.split('.')[1]) {
			if (nums.value.split('.')[1].length >= 2) {
				return false
			}
		}
		// this.nums.length
		console.log('this.nums.length', nums.value.length)

		nums.value = nums.value + item.text
		if (nums.value.length > 6) {
			nums.value = nums.value.slice(0, 6)
			uni.showToast({
				title: '最多只能输入6位数哦',
				icon: 'none'
			});
			return;
		}
	}
	const getListitemstyle = (index) => {
		return {
			background: numbers[index].background
		};
	}

希望可以帮助到漂泊在外的程序员们!!!

uniapp是一个基于Vue.js的跨平台开发框架,而Vue3是Vue.js的最新版本。它们的结合可以带来更好的开发体验和性能优化。uniapp提供了导航栏双页切换的功能,而Vue3的Pinia框架则提供了参数输出和方法调用的功能,还支持模块化和持久化存储。如果您有关于uniappVue3的使用问题,您可以查看uniapp官网获取更详细的信息和示例代码。uniapp在早期版本中开始支持Vue3的使用,并且从HBuilderX 3.2版本开始支持创建基于Vue3的uniapp项目。使用uniappVue3开发项目的流程与常规的Vue3开发类似。您可以按照官方提供的升级指南和使用文档来进行开发。希望以上信息对您有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [uniapp+vue3+pinia框架(模块化+持久化存储)](https://download.csdn.net/download/qq_35079107/87910679)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [uniapp + vue3微信小程序开发(1)框架搭建](https://blog.csdn.net/qq_39404437/article/details/124345386)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [uniapp开发uniapp之切换vue3,一直使用一直爽](https://blog.csdn.net/qq_42961150/article/details/121375073)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值