uniapp 小程序keyboard数字键盘 仿微信转账时的数字键盘

背景

由于要还原微信转账时的数字键盘,组件库里的 keyboard不太满足我的需求,就自己实现了个。

在输入时也做了限制如:

  • 输入 "01"  - "09" 的字符串时,赋值时会自动 去除前面的 0
  • 输入 "00" 时,赋值时 自动 转为 "0"
  • 限制小数点个数,不能连续输入 多个小数点
  • 限制小数个数只有两位

效果图

键盘代码如下

			<!-- 键盘 -->
			<view class="keyboard-wrap">
				<view class="keyboard-left">
					<view 
					:class="{'w_370': index === 9, 'w_normal': index !== 9}"
					class="keyboard-item key-item-h"
					hover-class="u-hover-class bg-gray"
					:hover-stay-time="100"
					@click="() => handleChange(getKeyValue(item,index))"
					v-for="(item,index) in 11" :key="index">
						<template v-if="item <= 9">
							{
  
  { index=== 9 ? 0 : index+1}}
							</template>
						<template v-else>.</template>
					</view>
				</view>
				<view class="keyboard-actions">
					<view class="keyboard-item w_normal key-item-h"
					hover-class="u-hover-class bg-gray"
					:hover-stay-time="100"
					@click="handleKeyboardBackspace">
						<u-icon name="close" size="28"></u-icon>
					</view>
					<view 
					class="keyboard-item w_normal key-submit"
					hover-class="u-hover-class bg-gray"
					:hover-stay-time="100">
						转账
					</view>
				</view>
			</view>
<script>
  export default {
   data() {
        priceValue: ""
    },
   methods: {
		getKeyValue(index) {
		  if (index === 10) {
		    return '.'; // 当 index 等于 10 时,返回 '.'
		  } else if (index === 9) {
		    return 0; // 当 index 等于 9 时,返回 0
		  } else {
		    return index + 1; // 其他情况,返回 index + 1
		  }
		},
        handleChange(val) {
            // 将 priceValue 转换为字符串以进行操作
			let priceValueStr = this.priceValue.toString();

			// 检查是否是小数点
			if (val === '.') {
			  // 如果当前字符串中还没有小数点,则添加小数点
			  if (!priceValueStr.includes('.')) {
			    priceValueStr += val;
			  }
			  // 如果已经包含小数点,不再添加小数点
			} else {
			  // 如果是数字,检查是否已经存在小数点
			  if (priceValueStr.includes('.')) {
			    // 获取小数点后的长度
			    let decimalPart = priceValueStr.split('.')[1];
			    // 如果小数点后的长度已经达到2位,则不再添加数字
			    if (decimalPart && decimalPart.length >= 2) {
			      return; // 阻止添加更多的数字
			    }
			    // 如果小数点后长度未达到2位,则添加数字
			    priceValueStr += val;
			  } else {
			    // 如果还没有小数点,添加数字
			    priceValueStr += val;
			  }
			}
		
			// 去掉 '01' 到 '09' 前面的 '0'
			if (val !== '0' && priceValueStr.length > 1 && priceValueStr[0] === '0' && priceValueStr[1] !== '.') {
			  priceValueStr = priceValueStr.substring(1);
			}

			// 更新 priceValue
			this.priceValue = priceValueStr;
        },
        // 删除
		handleKeyboardBackspace() {

			if(this.priceValue.length) this.priceValue = this.priceValue.substr(0,         
             this.priceValue.length - 1);

		},
    }
}
</script>
<style lang="scss">
.keyboard-wrap {
	display: flex;
	padding: 10rpx;
	.keyboard-left {
		width: 80%;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
	}
	.w_370 {
		width: 358rpx;
	}
	.w_normal {
		width: 170rpx;
	}
	.key-item-h {
		height: 90rpx;
	}
	.keyboard-actions {
		.key-submit {
			margin-top: 8px;
			background: #07c160;
			color: #fff;
			height: 156px;
		}
	}
	.keyboard-item {
		background-color: #fff;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		border-radius: 8px;
		margin: 8rpx 4rpx;
		font-weight: bold;
		font-size: 32rpx;
	}
	.bg-gray {
		background: #bbbcc6;
	}
}
</style>

### 如何在 UniApp 中创建类似于微信的充值键盘 #### 创建自定义组件 为了模拟微信支付键盘,在项目中新建一个名为 `RechargeKeyboard` 的 Vue 组件文件夹并添加相应的模板结构[^1]。 ```vue <template> <view class="keyboard"> <!-- 键盘布局 --> <view v-for="(row, index) in keyboardLayout" :key="index" class="row"> <block v-for="(item, idx) in row" :key="idx"> <button @click="handleClick(item)" :class="[item === 'delete' ? 'btn-delete' : '', item === '.' || !isNaN(item) ? 'number-btn' : '']">{{ item }}</button> </block> </view> </view> </template> <script> export default { data() { return { // 定义键盘按键分布 keyboardLayout: [ ['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['', '0', 'delete'] ] }; }, methods: { handleClick(value) { this.$emit('inputChange', value); } } }; </script> <style scoped> .keyboard .row button { /* 样式省略 */ } .number-btn, .btn-delete { /* 样式省略 */ } </style> ``` #### 使用自定义事件处理输入逻辑 通过监听子组件发出的 `inputChange` 自定义事件来更新父级页面中的金额数值显示区域的内容[^2]。 ```javascript // pages/recharge/index.vue import RechargeKeyboard from '@/components/RechargeKeyboard'; export default { components: { RechargeKeyboard }, data() { return { amountText: '' }; }, methods: { handleKeyInput(keyValue) { if (keyValue === "delete") { this.amountText = this.amountText.slice(0, -1); // 删除最后一位字符 } else if (!isNaN(parseFloat(keyValue)) && keyValue !== '') { this.amountText += keyValue; // 追加数字到字符串结尾处 } } } } ``` #### 页面样式调整 针对不同平台特性优化视觉效果,确保用户体验一致性和美观度。可以利用条件编译语法适配各端差异化的 CSS 属性设置[^3]。 ```css /* 支付密码键盘 */ .uni-keyboard { position: fixed; bottom: 0; width: 100%; background-color: white; &__content { display: flex; justify-content: space-between; > div { padding: 20rpx; &:nth-child(even){ color:#FFCC00; } &[data-type=del]{ font-size: larger; } } } } /* H5 平台特有样式 */ @media screen and (-webkit-min-device-pixel-ratio:0) { .uni-keyboard[data-v-xxxxxx] { border-top-left-radius: 20rpx; border-top-right-radius: 20rpx; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值