1. 单行输入组件(input)
input组件用于录入单行文本,尽管input的基本功能是文本录入,但该组件的属性还是比较多的,也比较复杂。下面是input属性的属性及其含义。
注意:这些属性中,auto-focus和focus目前只能在真机上测试。
下面的布局代码演示了这些属性 常用使用方法。
<view style="margin:20px">
<input placeholder="请输入你的名字" value="默认值" />
<input placeholder-style="color:red" placeholder="占位符字体是红色的" auto-focus/>
<input style="margin-top:20px" placeholder="这个只有在按钮点击的时候才聚焦" focus="{{focus}}" />
<button bindtap="bindButtonTap">使得输入框获取焦点(在真机上测试)</button>
<input style="margin-top:20px" maxlength="5" placeholder="最大输入长度5" />
<view style="margin-top:20px">你输入的是:{{inputValue}}</view>
<input bindinput="bindKeyInput" placeholder="输入同步到view中" />
<input style="margin-top:20px" bindinput="bindReplaceInput" placeholder="将<和>进行转义" />
<input style="margin-top:20px" bindinput="bindHideKeyboard" placeholder="输入close自动收起键盘" />
<input style="margin-top:20px" type="emoji" placeholder="这是一个带有表情的输入框" />
<input type="digit" placeholder="带小数点的数字键盘" />
<input type="idcard" placeholder="身份证输入键盘" />
<input password="true" placeholder="请输入密码" />
</view>
显示的效果如图1所示。
图1 input显示效果
在布局代码中,通过bindinput事件校验用的输入,如果输入close,则关闭键盘(需要在真机上测试,模拟器不支持软键盘)。input还支持几种输入类型,如数字、身份证、表情等,这些输入类型,并不是指不能输入其他的字符,而是指软键盘的乐行,例如,数字输入类型,弹出的是输入输入键盘(只包含10个数字键和其他几个字符的软键盘)。图2是弹出的身份证输入类型(左下角多了一个x键,和数字键盘类似)。
图2 身份证键盘
图3是输入表情的软键盘。
图3 输入表情的软键盘
图4是弹出的默认软键盘。
图4 默认的软键盘
完整的实现代码如下:
Page({
data:{
focus:false,
inputValue:""
},
bindButtonTap:function(){
this.setData({
focus:true
})
},
bindKeyInput:function(e){
this.setData({
inputValue:e.detail.value
})
},
// 当输入<和>是,会自动转换为<和>
bindReplaceInput:function(e){
var value = e.detail.value;
var pos = e.detail.cursor;
if(pos != -1){
//光标在中间
var left = e.detail.value.slice(0,pos);
//计算光标的位置
pos = left.replace(/</g,'<').replace(/>/g,'>').length;
}
//直接返回对象,可以对输入进行过滤处理,同时可以控制光标的位置
return {
value:value.replace(/</g,'<').replace(/>/g,'>'),
cursor:pos
}
},
bindHideKeyboard:function(e){
if(e.detail.value === "close"){
//收起键盘
wx.hideKeyboard();
}
}
})
2. 多行文本组件(textarea)
textarea允许输入多行文本,如果文本行数超过textarea组件的高度,会出现垂直滚动条。textarea有如下几个属性。
下面的布局代码演示了textarea组件的基本用法,由于第一个textarea组件设置了auto-height属性,所以该组件会随着行数的增加而变高。
<view style="margin:20px">
<textarea bindblur="bindTextAreaBlur" auto-height placeholder="自动变高" style="background:#ff0"/>
</view>
<view style="margin:20px">
<textarea placeholder="placeholder颜色是红色的" placeholder-style="color:red;" />
</view>
<view style="margin:20px">
<textarea placeholder="这是一个可以自动聚焦的textarea" auto-focus />
</view>
<view style="margin:20px">
<textarea placeholder="这个只有在按钮点击的时候才聚焦" focus="{{focus}}" />
<view class="btn-area">
<button bindtap="bindButtonTap">使得输入框获取焦点</button>
</view>
</view>
布局的显示效果如图5所示。
图5 textarea的显示效果
如果在第一个textarea组件中不断输入新行,那么textarea组件的高度会不断增加,效果如图6所示。
图6 不断增加新行的textarea组件
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有