2.相关代码使用组件形式实现键盘输入组件代码index.wxml
<view class="carplate" wx:if="{{show}}"> <block wx:if="{{type==1}}"> <view class="wordlist"> <view class="worditem" wx:for="{{citykeyword1}}" wx:key="{{item}}" bindtap="handleclick" data-type="1" data-item="{{item}}">{{item}}</view> </view> <view class="wordlist"> <view class="worditem" wx:for="{{citykeyword2}}" wx:key="{{item}}" bindtap="handleclick" data-type="1" data-item="{{item}}">{{item}}</view> </view> <view class="wordlist"> <view class="worditem" wx:for="{{citykeyword3}}" wx:key="{{item}}" bindtap="handleclick" data-type="1" data-item="{{item}}">{{item}}</view> </view> <view class="wordlist"> <view class="worditem" wx:for="{{citykeyword4}}" wx:key="{{item}}" bindtap="handleclick" data-type="1" data-item="{{item}}">{{item}}</view> </view> </block> <block wx:else> <view class="wordlist"> <view class="worditem" wx:for="{{keynumber}}" wx:key="{{item}}" bindtap="handleclick" data-type="2" data-item="{{item}}">{{item}}</view> </view> <view class="wordlist"> <view class="worditem" wx:for="{{wordlist1}}" wx:key="{{item}}" bindtap="handleclick" data-type="2" data-item="{{item}}">{{item}}</view> </view> <view class="wordlist"> <view class="worditem" wx:for="{{wordlist2}}" wx:key="{{item}}" bindtap="handleclick" data-type="2" data-item="{{item}}">{{item}}</view> <view class="worditem wordclear" bindtap="handleclick" data-item="delete"> <image src="/images/input-clear.png" class="clearimg"></image> </view> </view> <view class="wordlist"> <view class="worditem" wx:for="{{wordlist3}}" wx:key="{{item}}" bindtap="handleclick" data-item="{{item}}">{{item}}</view> <view class="worditem wordconfirm" bindtap="handleclick" data-item="confirm">确定</view> </view> </block></view>
index.css.carplate{ position: fixed; padding: 12rpx 12rpx 30rpx; left: 0; bottom: 0; width: 100%; /* height: 150px; */ font-size: 30rpx; background: #fff; box-sizing: border-box; border-top: 1px solid rgb(211, 207, 207); z-index: 200;}.wordlist{ display: flex; width: 100%; justify-content: space-between; align-items: center;}.worditem{ margin: 5rpx; width: 70rpx; height: 70rpx; line-height: 70rpx; text-align: center; border: 1px solid #eee; border-radius: 10rpx;}.wordconfirm{ width: 130rpx; color: #fff; background: #473af0;}.wordclear{ width: 100rpx;}.clearimg{ width: 60rpx; height: 60rpx; vertical-align: middle;}
index.jscomponent({ properties: { type: { type: number, default: 1, }, show: { type: boolean, default: false, } }, data: { citykeyword1: '京沪浙苏粤鲁晋冀豫', citykeyword2: '川渝辽吉黑皖鄂湘赣', citykeyword3: '闽陕甘宁蒙津贵云', citykeyword4: '桂琼青新藏港澳台', keynumber: '1234567890', wordlist1: 'qwertyuiop', wordlist2: 'asdfghjkl', wordlist3: 'zxcvbnm', }, methods: { handleclick(e) { let value = e.currenttarget.dataset.item; let type = e.currenttarget.dataset.type; switch(value) { case 'confirm': this.triggerevent('confirm'); break; case 'delete': this.triggerevent('delete'); break; default: this.triggerevent('change', { value, type }); } } }})
3.父组件引入我想实现点击输入后有上拉的效果,开始我想使用offset来实现的,但是下班后洗衣服想了下,不太好实现,我就想到了我以前做购物车时,有用到transform,原理差不多,我就把他用上了然后就是点击键盘外实现收起键盘,开始我想到的就是在父组件的最外层定义关闭事件,父级里面的盒子都使用catch方法阻止冒泡,但想下阻止冒泡好像又有点不合情理,就又把阻止冒泡给去掉了父组件index.wxml<view class="container" bindtap="handleplateconfirm"> <view class="translateview" style="transform: translatey({{translatespace}}px)"> <view class="list"> <view class="item"> <view class="label">*车牌号码</view> <view class="contentbox" catchtap="handleclick"> <view class="inputbox" wx:if="{{carno}}">{{carno}}</view> <view class="prompttext" wx:else>请输入车牌号</view> </view> </view> </view> </view></view><car-plate show="{{showplateinput}}" bindchange="handleplatechange" type="{{inputtype}}" bindconfirm="handleplateconfirm" binddelete="handleplatedelete" />
父组件index.jspage({ data: { carno: '', translatespace: 0, inputtype: 1, // 车牌输入类型,1简称,2数字或者字母, showplateinput: false, }, /* 用于点击弹出键盘输入,space为键盘弹出后向上拉取的距离 */ handleclick(e) { /* 150为键盘的高度 */ let space = -(e.currenttarget.offsettop - 150); /* regexp用于判断当前已输入的车牌号是否是中文,并让键盘显示中文还是英文输入 */ let regexp = /^[\u4e00-\u9fa5]+/; let inputtype = 1; if(regexp.test(this.data.carno)) { inputtype = 2; } this.setdata({ translatespace: space, showplateinput: true, inputtype }) }, /* 键盘输入操作 */ handleplatechange(e) { let value = e.detail.value; let type = e.detail.type; let carno = this.data.carno; carno += value; if(type == 1) { this.setdata({ inputtype: 2 }) } this.setdata({ carno }) }, /* 点击键盘上的确定 */ handleplateconfirm() { /* iscarplate用于判断输入的车牌号是否符合规范 */ if (!this.iscarplate(this.data.carno)) { wx.showtoast({ title: '请输入正确的车牌号', icon: 'none', duration: 2000 }) return false; } this.setdata({ translatespace: 0, showplateinput: false, inputtype: 1 }) }, /* 用于键盘输入删除 */ handleplatedelete(e) { let carno = this.data.carno; carno = carno.substring(0, carno.length - 1); if(carno.length == 0) { this.setdata({ inputtype: 1 }) } this.setdata({ carno, }) }, /* 判断车牌号 */ iscarplate(value) { return /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][a-z](([0-9]{5}[df])|([df]([a-hj-np-z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][a-z][a-hj-np-z0-9]{4}[a-hj-np-z0-9挂学警港澳使领]))$/.test(value); }})
父组件index.css.container{ height: 100vh; background: #fff;}.translateview{ background: #eee;}.list{ margin-bottom: 20rpx; background: #fff;}.list:last-child{ margin: 0;}.item{ display: flex; padding: 0 26rpx; width: 100%; height: 116rpx; box-sizing: border-box; align-items: center; border-bottom: 1px solid #eee;}.item:last-child{ border: none;}.label{ margin-right: 10rpx; width: 140rpx;}.contentbox{ display: flex; width: calc(100% - 150rpx); height: 90rpx; align-items: center; justify-content: space-between;}.prompttext{ color: #c7c7c7;}.inputbox{ width: 100%; height: 80rpx; line-height: 80rpx;}
正在努力学习中,若对你的学习有帮助,留下你的印记呗(点个赞咯^_^)推荐教程:《微信小程序》
以上就是微信小程序怎么使用车牌号输入法的详细内容。
