参考这个链接:uniapp 自定义数字键盘_uniapp 数字键盘-CSDN博客
这个也可以看看:uniapp 手机端时禁止输入框弹出键盘,使用自定义键盘_uniapp禁用手机键盘-CSDN博客
效果图:
代码如下:
<template>
<view style="background-color: white; min-height: 1200rpx;">
<view
style="width: 100%; height: 100rpx; display: flex; justify-content: center; align-items: center; background-color: #4CAF50;">
<view style="color: white; font-size: 40rpx; font-weight: 600;">会员登录</view>
</view>
<view style="width: 100%; height: 100rpx; z-index: 99; margin-top: 50rpx;">
<view class="" style="display: flex; justify-content: center; align-items: center;">
<input v-model="clockweightdata" placeholder="请输入会员手机号" type="text" :disabled="showKeyboard" @click="showKeyboard = true"
style="width: 700rpx; height: 80rpx; border: 3rpx solid #4CAF50; margin: 0 20rpx; border-radius: 30rpx; text-align: center;">
</view>
</view>
<!-- 增加第二个输入框,用于输入密码 -->
<view style="width: 100%; height: 100rpx; z-index: 99; margin-top: 50rpx;">
<view class="" style="display: flex; justify-content: center; align-items: center;">
<input v-model="passwordData" placeholder="请输入会员密码" type="password" :disabled="showKeyboard" @click="showKeyboard = true"
style="width: 700rpx; height: 80rpx; border: 3rpx solid #4CAF50; margin: 0 20rpx; border-radius: 30rpx; text-align: center;">
</view>
</view>
<view
style="width: 100%; display: flex; flex-wrap: wrap; justify-content: center; align-items: flex-start; overflow: auto; margin-top: 200rpx;"
v-if="showKeyboard">
<!-- 键盘数字按钮 -->
<view class="but" v-for="item in 12" :key="item" @click="keyboard(item)">
<label>{{ item === 10 ? '.' : item === 11 ? '0' : item === 12 ? '' : item }}</label>
</view>
<!-- 操作按钮 -->
<view class="but" @click="deleteButtonClicked" style="background-color: #ff7c70;">删除</view>
<view class="but" @click="clearButtonClicked" style="background-color: #ff7c70;">清空</view>
<view class="but" @click="confirm" style="background-color: #4CAF50;">确定</view>
</view>
<view class="list recommend" v-else>
<view style="padding-top: 10px;"></view>
<view class="con"
@click="toUrl(`/pages/store/teacher/detail?id=${technician.id}&categoryId=${getType(technician.categoryId)}`)">
<image src="../../static/logo.png" mode="heightFix" class="img"></image>
<view class="right">
<view class="title">名字</view>
<view class="intro">注册地址:</view>
<view class="" style="text-align: right; padding-top: 5px; padding-right: 10%;">
<button type="primary" size="mini"> 点击查看预约 </button>
</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
clockweightdata: '', // 会员手机号
passwordData: '', // 会员密码
showKeyboard: true // 初始状态下显示键盘
};
},
onReady() {},
onLoad() {},
methods: {
// 自定义键盘事件
keyboard(item) {
// 点号
if (item === 10 && this.clockweightdata !== '') {
const x = String(this.clockweightdata).indexOf('.') + 1;
if (x !== 0) {
return;
}
const num = this.clockweightdata;
const key = '.';
this.clockweightdata = num + key;
return;
}
// 1-9
const x = String(this.clockweightdata).indexOf('.') + 1;
const y = String(this.clockweightdata).length - x;
if (x !== 0 && y >= 2) {
return;
}
if (item <= 9) {
const num = this.clockweightdata;
const key = String(item);
this.clockweightdata = num + key;
return;
}
// 0
if (item === 11 && this.clockweightdata !== '') {
const num = this.clockweightdata;
const key = '0';
this.clockweightdata = num + key;
}
},
// 删除
deleteButtonClicked() {
if (this.clockweightdata.length > 0) {
this.clockweightdata = this.clockweightdata.slice(0, -1);
}
},
//清空
clearButtonClicked() {
this.clockweightdata = '';
},
// 确定
confirm() {
// 处理确定按钮的点击事件,您可以在这里执行您的操作
// 例如,提交数据或执行其他操作
if (this.clockweightdata.trim() === '') {
// 输入框为提示用户输入内容
uni.showToast({
title: '请输入内容',
icon: 'none'
});
} else {
this.showKeyboard = false;
console.log('点击了确定按钮');
}
},
toUrl(url) {
// 实现跳转逻辑,你可以根据自己的需求来实现
console.log('跳转到', url);
},
getType(categoryId) {
// 实现获取类型逻辑,你可以根据自己的需求来实现
return categoryId;
}
}
};
</script>
<style lang="scss" scoped>
.but {
width: 30%;
height: 150rpx;
text-align: center;
line-height: 150rpx;
border: 2rpx solid #fff;
}
.list,
.recommend {
margin-top: 40rpx;
.con {
background-color: white;
display: flex;
justify-content: space-between;
border-radius: 10px;
margin: 0 36rpx 20rpx 36rpx;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.4);
.img {
width: 90px;
height: 90px;
border-top-left-radius: 20rpx;
border-bottom-left-radius: 20rpx;
}
.right {
position: relative;
width: calc(100% - 110px);
uni-view {
font-size: 24rpx;
color: #575858;
}
.title {
color: #101010;
font-size: 28rpx;
font-weight: bold;
margin-top: 10rpx;
word-break: break-all;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
overflow: hidden;
}
}
}
}
</style>
又重新加了个输入框的,加了一些逻辑
效果如下:
代码如下:
<template>
<view style="background-color: white; min-height: 1200rpx;">
<view
style="width: 100%; height: 100rpx; display: flex; justify-content: center; align-items: center; background-color: #4CAF50;">
<view style="color: white; font-size: 40rpx; font-weight: 600;">会员登录</view>
</view>
<view style="width: 100%; height: 100rpx; z-index: 99; margin-top: 50rpx;">
<view class="" style="display: flex; justify-content: center; align-items: center;">
<!-- 会员手机号输入框,添加点击事件来设置激活状态 -->
<input v-model="clockweightdata" placeholder="请输入会员手机号" autocomplete="off-new" type="text" @click="setActiveInput('phone')"
style="width: 700rpx; height: 80rpx; border: 3rpx solid #4CAF50; margin: 0 20rpx; border-radius: 30rpx; text-align: center;">
</view>
</view>
<!-- 增加第二个输入框,用于输入密码 -->
<view style="width: 100%; height: 100rpx; z-index: 99; margin-top: 50rpx;">
<view class="" style="display: flex; justify-content: center; align-items: center;">
<!-- 会员密码输入框,添加点击事件来设置激活状态 -->
<input v-model="passwordData" placeholder="请输入会员密码" autocomplete="new-text" type="password" @click="setActiveInput('password')"
style="width: 700rpx; height: 80rpx; border: 3rpx solid #4CAF50; margin: 0 20rpx; border-radius: 30rpx; text-align: center;">
</view>
</view>
<view
style="width: 100%; display: flex; flex-wrap: wrap; justify-content: center; align-items: flex-start; overflow: auto; margin-top: 100rpx;"
v-if="showKeyboard">
<!-- 键盘数字按钮 -->
<view class="but" v-for="item in 12" :key="item" @click="keyboard(item)">
<label>{{ item === 10 ? '.' : item === 11 ? '0' : item === 12 ? '' : item }}</label>
</view>
<!-- 操作按钮 -->
<view class="but" @click="deleteButtonClicked" style="background-color: #ff7c70;">删除</view>
<view class="but" @click="clearButtonClicked" style="background-color: #ff7c70;">清空</view>
<view class="but" @click="confirm" style="background-color: #4CAF50;">确定</view>
</view>
<view class="list recommend" v-else>
<view style="padding-top: 10px;"></view>
<view class="con"
@click="toUrl(`/pages/store/teacher/detail?id=${technician.id}&categoryId=${getType(technician.categoryId)}`)">
<image src="../../static/logo.png" mode="heightFix" class="img"></image>
<view class="right">
<view class="title">名字</view>
<view class="intro">注册地址:</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
clockweightdata: '', // 会员手机号
passwordData: '', // 会员密码
activeInput: 'phone', // 当前激活的输入框
showKeyboard: true // 键盘的显示状态
};
},
created(){
this.clockweightdata = '';
this.passwordData = '';
},
onReady() {},
onLoad() {},
methods: {
// 设置当前激活的输入框
setActiveInput(inputType) {
this.activeInput = inputType;
this.showKeyboard = true; // 无论何时点击输入框,都显示键盘
},
// 自定义键盘事件
keyboard(item) {
// 处理数字按钮的输入 (1-9 和 0)
if (item >= 1 && item <= 9) { // 对应数字 1-9
if (this.activeInput === 'phone') {
this.clockweightdata += item.toString(); // 向电话号码添加数字
} else if (this.activeInput === 'password') {
this.passwordData += item.toString(); // 向密码添加数字
}
} else if (item === 11) { // 对应数字 0
if (this.activeInput === 'phone') {
this.clockweightdata += '0'; // 向电话号码添加0
} else if (this.activeInput === 'password') {
this.passwordData += '0'; // 向密码添加0
}
} else if (item === 10) { // 对应点号 '.'
// 仅电话号码输入框处理点号
if (this.activeInput === 'phone' && this.clockweightdata.indexOf('.') === -1) {
this.clockweightdata += '.'; // 在电话号码中添加点号,但仅当它尚不存在时
}
}
},
// 删除和清空操作也需要修改,以处理当前激活的输入框
deleteButtonClicked() {
if (this.activeInput === 'phone' && this.clockweightdata.length > 0) {
this.clockweightdata = this.clockweightdata.slice(0, -1);
} else if (this.activeInput === 'password' && this.passwordData.length > 0) {
this.passwordData = this.passwordData.slice(0, -1);
}
},
//清空
clearButtonClicked() {
if (this.activeInput === 'phone') {
this.clockweightdata = '';
} else {
this.passwordData = '';
}
},
// 确定
confirm() {
// 处理确定按钮的点击事件,您可以在这里执行您的操作
// 例如,提交数据或执行其他操作
if (this.clockweightdata.trim() === '') {
// 输入框为提示用户输入内容
uni.showToast({
title: '请输入会员手机号',
icon: 'none'
});
} else if(this.passwordData.trim() === ''){
// 输入框为提示用户输入内容
uni.showToast({
title: '请输入会员密码',
icon: 'none'
});
}else{
this.showKeyboard = false;
console.log('点击了确定按钮');
}
},
toUrl(url) {
// 实现跳转逻辑,你可以根据自己的需求来实现
console.log('跳转到', url);
},
getType(categoryId) {
// 实现获取类型逻辑,你可以根据自己的需求来实现
return categoryId;
}
}
};
</script>
<style lang="scss" scoped>
.but {
width: 30%;
height: 150rpx;
text-align: center;
line-height: 150rpx;
border: 2rpx solid #fff;
}
.list,
.recommend {
margin-top: 40rpx;
.con {
background-color: white;
display: flex;
justify-content: space-between;
border-radius: 10px;
margin: 0 36rpx 20rpx 36rpx;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.4);
.img {
width: 90px;
height: 90px;
border-top-left-radius: 20rpx;
border-bottom-left-radius: 20rpx;
}
.right {
position: relative;
width: calc(100% - 110px);
uni-view {
font-size: 24rpx;
color: #575858;
}
.title {
color: #101010;
font-size: 28rpx;
font-weight: bold;
margin-top: 10rpx;
word-break: break-all;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
overflow: hidden;
}
}
}
}
</style>